Basic Program Structure in C#.NET Programs



Watch the video or follow the tutorial.



All C# programs contain certain code that serves as the basis for creating a windows program, some of this code is as follows:

Program References
Using Statements
Namespaces
Classes
Regions
Comments
The Main Method

Let's now have a closer look at each of the program components. First of all open Visual Studio and Create New Project, then select Visual C# - Windows Classic Desktop - Windows Forms App (.NET Framework). Change the location of the project folder to whatever you want and name the app BasicProgramStructure, Press OK.


When the project opens it will show the default window, select Form1 in the Solution Explorer and press F7. The code window will open and show the default code.


Program References

References are used to insert external code into your project so that much of the stuff you will use is already written for you by Microsoft and hence you don't have to write it yourself. For example, any C# project that uses windows forms is going to use the System.Windows.Forms reference because it contains all the pre-built code that Microsoft distributes with Visual Studio. In fact, there are pre-existing references for pretty much all aspects of windows development: XML, DirectX, TCP, and such like so in order to use any particular code you simply add it as a reference to your project and then all its components are available for you to use.


Adding a reference is shown below


Right-click on References and select 'Add Reference'




Select the reference that to want to add and tick the checkbox on the left hand side and press OK. You can add as any references as you like at one time.


Once a reference has been added it is ready to be used in the code using a using statement.



Using Statements

Using statements are import commands that insert the code from the references that are included in the project and make its contents available to you from anywhere in the code after the using statement itself. They are placed at the start (top) of the .cs code file and during compile time the actual code from the references is inserted in its place.
An example of a using statement would be as follows:

using System.Windows.Forms;

This statement allows all objects, functions, etc in the Forms group to be available for you to use in your code and intellisense will show you the correct code as you begin to type. Alternatively you can still use these object without adding a using statement by typing System.Windows.Forms at every instance of object use. so with the using statements at the top of the document, to create a timer you would write:


Timer newTimer = new Timer();


Whereas without the using statement added you would write:


System.Windows.Forms.Timer newTimer = new System.Windows.Forms.Timer();


Both these lines of code are valid but as you can see adding the using statement prevents you from having to write a lot of unnecessary code that would be a waste of time.


Namespaces

Namespaces control access to particular bits of code within your application from other areas of your application. It allows you prevent naming conflicts by assigning sections of your code with a specific namespace and then making them accessible only through using that namespace. This means that a class called frmMain that exists in two places of your code but with different namespaces can coexist as long as you reference them with the correct namespace. Namespaces are a good way of organising your code and controlling its accessibility.


Classes

Classes are containers that hold multiple objects and group them together because they are all interconnected in some way. Classes can have variables, functions and properties that describe the basic purpose of the class. An example of this would be a class called Car which has properties called Color, Size and Position and functions called MoveForward and Reverse. As the program runs these functions would be called and properties would be modified and they would in turn update internal variables that hold information about the physical characteristics of that particular instance of the Car class.





As you can see the Car class has the System using statement that adds all the basic C# objects. It has the default namespace for the app so that all code is available to it. It has two private internal variables where private means only available to code within this class that holds the x and y position for the car. It has a function Move that will modify its position based upon the two parameters that it takes as arguments and it has two properties that will modify or return the current x and y positions for the car class.


Regions

Next we have regions, which quite simply are sections of code that you can collapse so that only the code you are working on will be visible, it is just a way of organising your code to make it easier to read. Regions are not added by visual studio by default you have to add them yourself and I start with four main regions for any app that I make that is going to take longer than an hour to complete.





As you can see regions can be quite useful in organising your code and their basic syntax is equally as succinct


#region Functions

Is the opening code and below is the closing code with the content in between


#endregion Functions

Comments

Comments allow the programmer to add notes to the code so that he can properly understand what the code is doing which can be especially important if you come back to the code after a break. I cannot stress enough how important it is to create detailed comments in your code, it not only helps yourself but others too who may look at your code one day.





Documentation Comments

A special kind of comment is the Documentation Comment which allows you to add a description to your function or property that will show up in intellisense when you later try to implement the function.





The image above shows the creation of a function called MyFunction and the documentation comments associated with it in green. By typing three forward slashes /// above the function the appropriate documentation comment items will appear and you only need to add the text you want between the triangular braces.





When implementing MyFunction in another section of code intellisense will display the text you wrote in the documentation comments to remind you of the purpose of the function and the meaning of the variables. This can be very useful and I always recommend that you make use of the documentation comments.



The Main Method or Function

ALL C# programs ave a main method. In a windows forms app it is not obvious however. If you go to the Solution Explorer and double click on Program.cs you will see the Main Function





As it says in the comment the main function is the entry point for the program and execution starts here. It only does a few things and I wouldn't advise changing anything, I just thought I would let you see where it is and what it contains. When Application.Run(new Form1()); executes it opens the window Form1 and waits for further commands. STAThread simply tells windows to run this app as a single threaded app.


A couple more notes about C# basic program structure. C# is case sensitive meaning a variable called var1 and another called Var1 will not conflict because the names are not the same. All statements and expressions must end with a semicolon ; at the end of the line. A line with just a semicolon and nothing else is a valid C# statement.


A full copy of the complete default code is shown below with the regions that I use added.



#region Using Statements

using System;
using System.Windows.Forms;

#endregion Using Statements

namespace BasicProgramStructure
{

public partial class Form1 : Form
{

#region Variables & Constants


#endregion Variables & Constants

#region Functions

public Form1()
{
InitializeComponent();
}


#endregion Functions

#region Properties


#endregion Properties

}
}


If the code is not properly indented when you paste it as shown above then press Ctrl + E,D that is hold down Ctrl and press E then D in quick succession, this will properly indent all code in the current document.


So I think that is all I would like to say about C# Basic Program Structure for this tutorial. In the next tutorial we will have a look at Data Types and Type Conversion, so until then, enjoy.