C# Basic Structure

As previously mentioned, C# is an Object-Oriented Programming (OOP) Language. Object Oriented Programming is based around the concept of objects, where an object is made up of Attributes, or Variables, that can contain data, and Methods, which describe actions of an object. A person can be used as a real-world example of an object. A person has attributes such as height, weight, eye colour, hair colour and so on, as well as methods or actions such as, walk, talk and eat.

A Class is a blue print, or template, of an object that describes its attributes and methods. From this template, multiple objects can be created. A Class can be compared to an architects drawing of a building, which describes how it needs to be built. From this drawing, one or more buildings can be made to the specification of the drawing.

The basic structure of a C# Class can be seen below. A C# program can contain one or more Classes. Within a Class or Classes of a program, there must be one Class with a Method called ‘main’, which is executed when the program starts.

// Class definition.
class Class_name
{
    /* Main method of a C# application.
    Excutes when the application starts. */
    static void Main(string[] args)
    {

        // Statement(s) to execute.

    }
}

The words in blue in the above example are known as reserved words and have a specific meaning in C#. These words cannot be used for anything other than the purpose that the language defines. The green lines of text are comments. When programming in any language it is useful to add comments to the code to describe what is happening and why. This is useful when the code is revisited at a later date to make enhancements, or for debugging purposes. Single line comments are preceded by ‘//’, whilst comments over multiple lines start with ‘/*’ and end ‘*/’.

The Class below is a simple example of a program that just outputs the message ‘Hello World!’ in a console window. The second line forces the user to press a key on the keyboard before the console window closes.

class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("Hello World!");
        Console.ReadKey();

    }
}

In order to run a program like the one above, you must first compile it. This process can vary depending on what you are using to create the program. To compile a program from the command line the following command can be used.

mcs filename.cs

After you have compiled your program you will get an additional file with the ‘.exe’ extension that is the same name as the code file. The program can now be run from the command line.

mono filename.exe

It is also possible to combine the compile and run commands in to one line.

mcs filename.cs && mono filename.exe

From within Microsoft Windows, it is also possible to run a program from the command line simply by specifying the name of the executable.

filename.exe

It should be noted that the full path would need to be specified if the program does not reside in the current directory.

Similarly, from within the directory where the program is located, it can be run as follows within a PowerShell window.

./filename.exe

If you are using an Integrated Development Environment, or IDE, such as Visual Studio, there are menu options to carry out these tasks.

Solutions and Projects

If you are using an IDE such as Visual Studio, it has the concepts of Solutions and Projects, where a Solution can contain one or more Projects. These can be created via the graphical user interface. It may be desirable to have more than one Project in a Solution, if, for example, it is required to unit test an application. In this instance, there would be one project for say a console application and a separate project to carry out the unit testing.

If an IDE such as Visual Studio Code is being used, which doesn't provide for the creation of Solutions and Projects through the graphical user interface, then, since .Net Core 2.1, there is a way to do this via the command line. This is done in PowerShell on Windows, or a Terminal window on Linux and Mac.

Before running the commands below, make sure that you are at the desired location, within your chosen command window, where the Solution and Project need to be created.

This first command creates the folder structure for the Solution and Project. The folder for the solution is named 'DemoConsoleSln', whilst the folder for the Project, within the Solution folder, is called 'DemoConsole'. Within the project folder a JSON file called 'global.json' is also created, which specifies the version of the Software Development Kit, or SDK, to use for this project, in this case, '3.1.402'.

dotnet new globaljson --sdk-version 3.1.402 --output DemoConsoleSln/DemoConsole

This next command creates a console application project, within the 'DemoConsole' folder and specifies the framework version to use, in this case .NET Core 3.1.

dotnet new console --output DemoConsoleSln/DemoConsole --framework netcoreapp3.1

The project folder now includes a project file, 'DemoConsole.csproj', containing information about the project and a 'Program.cs' file, which is a C# file, containing the code for a basic 'Hello World' console application.

Next the solution has to be created, within the 'DemoConsoleSln' folder.

dotnet new sln -o DemoConsoleSln

The Solution folder now contains a file called 'DemoConsoleSln.sln', which contains information about the Solution.

The final step is to add the Project to the Solution.

dotnet sln DemoConsoleSln add DemoConsoleSln/DemoConsole

As well as being able to create Solutions and Projects, .NET Core 2.1 and above also allows for the building and running of projects from the command line. The following can be used to build a project without running it. Note that this command must be run from within the project folder, in this case 'DemoConsole'.

dotnet build

It is also possible to build and run a project using a single command, again from within the project folder.

dotnet run