Home >Backend Development >C#.Net Tutorial >[c# tutorial] C# program structure
C# Program Structure
Before we learn the basic building blocks of the C# programming language, let us first look at the minimal program structure of C# as a reference for the following chapters.
C# Hello World Example
A C# program mainly includes the following parts:
Namespace declaration
A class
Class method
Class attribute
A Main method
Statements & Expressions
Comments
Let Let's look at a simple code that prints "Hello World":
using System; namespace HelloWorldApplication { class HelloWorld { static void Main(string[] args) { /* 我的第一个 C# 程序*/ Console.WriteLine("Hello World"); Console.ReadKey(); } } }
When the above code is compiled and executed, it will produce the following results:
Hello World
Let's look at the above Various parts of the program:
The first line of the program using System; - The using keyword is used to include the System namespace in the program. A program generally has multiple using statements.
The next line is the namespace declaration. A namespace is a series of classes. The HelloWorldApplication namespace contains the class HelloWorld.
The next line is the class declaration. Class HelloWorld contains data and method declarations used by the program. Classes generally contain multiple methods. Methods define the behavior of a class. Here, the HelloWorld class has only one Main method.
The next line defines the Main method, which is the entry point for all C# programs. The Main method describes what the class will do when executed.
The next line /*...*/ will be ignored by the compiler, and it will add additional comments to the program. The
Main method specifies its behavior through the statement Console.WriteLine("Hello World");.
WriteLine is a method of the Console class defined in the System namespace. This statement displays the message "Hello, World!" on the screen.
The last line Console.ReadKey(); is for VS.NET users. This causes the program to wait for a key press, preventing the screen from quickly running and closing 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 name of the class.
Compile & Execute a C# Program
If you use Visual Studio.Net to compile and execute a C# program, follow these steps:
Start Visual Studio.
On the menu bar, select File -> New -> Project.
Select Visual C# from Templates, then select Windows.
Select Console Application.
Give your project a name and click the OK button.
The new project will appear in Solution Explorer.
Write 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 a C# program:
Open a text editor and add the code mentioned above.
Save the file as helloworld.cs.
Open the command prompt tool and navigate to the directory where the file is saved.
Type csc helloworld.cs and press enter to compile the code.
If there are no errors in the code, the command prompt will enter the next line and generate the helloworld.exe executable file.
Next, type helloworld to execute the program.
You will see "Hello World" printed on the screen.
The above is the content of [c# tutorial] C# program structure. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!