C# Program Structure
Before we learn the basic 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 attributes
- A Main method
- Statements & Expressions
- Notes
C# files have the extension .cs.
The following creates a test.cs file, which contains simple code that prints "Hello World":
test.cs file code:
using
System
;
namespace
HelloWorldApplication
{
class
HelloWorld
{
static
void
Main
(
string
[
]
args
)
{
/* My first C# program */
Console
.
WriteLine
(
"Hello World"
)
;
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
Hello World
Let's look at the various parts of the program above:
- The first line of the program using System; - using The keyword is used to include the **System ** namespace in a program. A program generally has multiple using if statement.
- The next line is the namespace declaration. A namespace contains a series of classes.HelloWorldApplication A namespace contains classes HelloWorld。
- The next line is the class declarations. A class HelloWorld contains the data and method declarations used by the program. A class generally contains multiple methods. Methods define the behavior of a class. Here, theHelloWorld class has only one ** Main** method.
- The next line defines the Main method, which is the entry pointof all C# programs. The **Main ** method specifies what the class will do when executed.
- The next line /.../ are ignored by the compiler and add extra Notes。
- Main method specifies its behavior through the statement **Console.WriteLine("Hello World"); **.
WriteLine is a method defined in the System namespace Console of the class. This statement displays the message "Hello World" on the screen.
- The last line Console.ReadKey(); is for VS.NET users. This makes the program 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 (;).
- Program execution begins with 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 these steps:
- Launch Visual Studio.
- On the menu bar, select File -> New -> Project.
- Select Visual C# from the templates, then select Windows.
- Select Console Application.
- Give your project a name, then click the OK button.
- The new project will appear in Solution Explorer.
- Write code in the Code Editor.
- Click the Run button or press F5 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 a C# program:
- Open a text editor and add the code mentioned above.
- Save the file as helloworld.cs。
- Open a command prompt and navigate to the directory where the file is saved.
- Type csc helloworld.cs and press the enter key to compile the code.
- If there are no errors in the code, the command prompt moves to the next line and generates helloworld.exe an executable file.
- Next, type helloworld to execute the program.
- You will see "Hello World" printed on the screen.