C# Program Structure
Before we learn the fundamental building blocks of the C# programming language, let's first look at the minimal program structure of C# as a reference for the following chapters.

C# Hello World Example
A C# program mainly consists of the following parts:
- Namespace declaration
- A class
- Class methods
- Class properties
- A Main method
- Statements & Expressions
- Comments
The extension of a C# file is.cs。
The following creates atest.csFile, the file contains simple code that can print "Hello World":
The code of the test.cs file:
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* My first C# program */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:
Hello World
Let's look at the various parts of the above program:
- The first line of the programusing System; - usingThe keyword is used to include [namespaces] in a programSystem Namespace. A program generally has multipleusingstatement.
- The next line isnamespaceDeclaration. AnamespaceIt contains a series of classes.HelloWorldApplicationNamespaces contain classesHelloWorld。
- The next line isclassDeclaration. ClassHelloWorldIt contains the data and method declarations used by the program. Classes generally contain multiple methods. Methods define the behavior of the class. Here,HelloWorldA class has only one Mainmethod.
- The next line definesMainmethod, which is the [entry point] of all C# programsentry point。Main The method describes what actions the class will perform when executed.
- The next line /*...*/ will be ignored by the compiler, and it will add extra in the program.Comments。
- The Main method, through the statementConsole.WriteLine("Hello World"); specifies its behavior.
WriteLineis a defined inSystemin the namespaceConsoleA method of the class. This statement will display the message "Hello World" on the screen.
- Last lineConsole.ReadKey();This is for VS.NET users. It causes the program to wait for a key press, preventing the screen from running and closing quickly when the program is launched from Visual Studio .NET.
The following points are worth noting:
- C# is case-sensitive.
- All statements and expressions must end with a semicolon (;).
- The execution of the program starts from the Main method.
- Unlike Java, the file name can be different from the class name.
Compiling & executing a C# program
If you use Visual Studio.Net to compile and execute a C# program, follow the steps below:
- Launch Visual Studio.
- On the menu bar, select File -> New -> Project.
- From the templates, select Visual C#, then select Windows.
- Select Console Application.
- Specify a name for your project, and then click the OK button.
- The new project will appear in the Solution Explorer.
- Write the code in the Code Editor.
- Click the Run button or press the F5 key to run the program. A Command Prompt window will appear, displaying Hello World.
You can also use the command line instead of the Visual Studio IDE to compile C# programs:
- Open a text editor and add the code mentioned above.
- Save the file ashelloworld.cs。
- Open the command prompt tool and navigate to the directory where the file is saved.
- Typecsc helloworld.csAnd press the enter key to compile the code.
- If there are no errors in the code, the command prompt will go to the next line, and generatehelloworld.exeexecutable file.
- Next, typehelloworldto execute the program.
- You will see "Hello World" printed on the screen.