Structure of a C# Program
Understand the fundamental structure of a C# program, including statements, code blocks, the Main entry point, and comments.
We'll cover the following...
Developers write C# source code in text files with a .cs extension, and the compiler then transforms these files into an executable format. These files contain instructions compiled into intermediate language (IL) code.
Statements and code blocks
Statements are the basic building blocks of C# source code. A statement can be an action, such as an arithmetic operation, a method invocation, or a variable declaration and assignment.
Console.WriteLine("This is a statement.");
Like in C and C++, every statement must be followed by a semicolon (;). Omitting the semicolon prevents the compiler from parsing the end of a statement, which stops the generation of the IL code.
We can combine a set of instructions into blocks of code by enclosing statements in curly braces ({}).
{Console.WriteLine("I am inside a block!");Console.WriteLine("Me too.");}
Lines 1–4: Curly braces define a scope containing two statements that execute sequentially.
Blocks can contain other blocks. Essentially, both a class and a method can be seen as code blocks. A class contains a method, and a method contains instructions within its curly braces.
Line 4: Begins the class definition.
Line 6: Begins the
Mainmethod.Lines 9–15: Defines a nested block, which contains yet another block inside it (lines 11–14).
Curly braces and blocks will become much more useful when we start using constructs like loops and methods.
The Main() method
Every executable .NET application requires an entry point, which is either an explicit Main method or a compiler-generated one in top-level programs. In larger applications or specific architectures, we often define this entry point explicitly as a method named Main().
All executable programs have a Main() method that tells the CLR where to begin execution.
Let’s experiment. We want to see what happens if we remove the Main() method in a classic class-based program or rename it and try to execute the program. Rename the Main() method in the following code to any valid name (like Hello) and try to run it.
Line 5: The method is named
Helloinstead ofMain.Result: Because there are no top-level statements and no method named
Main, the compiler cannot find an entry point.
Notice the following error:
error CS5001: Program does not contain a static 'Main' method suitable for an entry point
The compiler generates this error when it cannot identify a valid entry point during the build process.
Comments
Comments are an important part of source code. They are ignored during compilation and are absent in the generated IL. They make code more readable and improve collaboration.
The following code snippet shows different types of comments in C#.
Lines 3–4: A multi-line comment describing the program. As we can see, we can write multi-line comments by putting the text between
/*and*/.Line 8: A single-line comment explaining the method. The
//denotes the beginning of a single-line comment. Any text after//is regarded as a comment and is ignored by the compiler.
Top-level programs
Modern C# allows for top-level programs, which simplifies the entry point by removing the need for a boilerplate class and method. The compiler generates the necessary entry point boilerplate automatically. This is the default style for simple console applications in .NET 10.
Line 1: A statement written directly at the root of the file, which the compiler treats as the entry point.