C # program structure
Before learning how to build C # programming language blocks, we should explore the basic C # program structure.
Create Hello World program in C #
A C # program includes the following sections:
- Declaring Namespace
- A class
- Class method
- Properties of the class
- A Main method
- Commands and expressions
- Comment
You follow the following simple code that will print "Hello World":
using System ; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { /* Code này sẽ in chữ Hello World trong C# */ Console . WriteLine ( "Hello World" ); Console . ReadKey (); } } }
Pressing F5 to compile and run the above C # program will produce the following results:
Now we consider the parts of the program above:
- The first line uses System ; The using keyword is used to include the System namespace in the program. In general, a program has many using statements.
- The next line has a namespace declaration. A namespace is a set of classes. HelloWorldApplication namespace contains HelloWorld class.
- The next line has a class declaration, HelloWorld class contains data definitions and methods that your program uses. In general, the class contains many methods. Methods for defining class behavior However, 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 represents the class state when executed.
- The next line / * . * / is ignored by the compiler and it is the comment for the program.
- Main method determines its behavior with Console.WriteLine command ("Hello World") ;
- WriteLine is a method of the Console class defined in the System namespace. This command makes the message "Hello, World!" is displayed on the screen.
- The last line Console.ReadKey () ; is for VS .NET Users. It makes the program wait for a key to be pressed and it prevents the screen from running and closing quickly when the program is launched from Visual Studio.Net.
You should keep in mind the following points:
- C # is case sensitive.
- All commands and expressions must end with a semicolon (;).
- The execution of the program starts at the Main method.
- Unlike Java, the program file name may be different from the class name.
Compile and execute C # programs
If you are using Visual Studio.Net to compile and execute C # programs, follow these steps:
- Open Visual Studio.
- On the menu bar, select File -> New -> Project .
- Select Visual C # from the Template, and then select Windows.
- Select Console Application.
- Specify a name for the project and click the OK button.
- This creates New Project in Solution Explorer.
- Write code in the Code Editor.
- Click the Run button or press F5 to execute the project . A Command Prompt window appears that contains the Hello World line.
You can compile a C # program using command-line instead of Visual Studio IDE:
- Open a Text Editor and add the above code.
- Save the file as helloworld.cs
- Open the Command Prompt tool and go to the folder where you saved the file.
- Compose csc helloworld.cs and press Enter to compile your code.
- If there are no errors in the code, the Command prompt takes you to the next line and creates the file helloworld.exe that can execute (executable).
- Create helloworld to implement your program.
You can see that the output is Hello World printed on the screen.
Follow tutorialspoint
Old post: Installing C # environment
New lesson: Basic C # syntax
You should read it
- Structure (Struct) in C programming
- Stack data structure (Stack)
- Basic C program structure
- What do you know about programming language C # P1
- Basic syntax of C programming
- Data structure in C / C ++
- What is the best URL structure for SEO?
- What is Data Structure?
- Test about program structure
- Queue data structure (Queue)
- Do you know what programming language is?
- Structure (Struct) in C #
Maybe you are interested
5 ways to quickly launch programs on Windows
10 tips to remove uninstallable programs
16 unnecessary Windows programs and applications you should uninstall
5 best programs to install on a new gaming PC
7 best laptop models for programmers
Huawei announced the 'Cangjie' programming language developed by the company