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
May be interested
- Basic C # syntaxc # is an object-oriented programming language. in an object-oriented programming method, a program of diverse objects that interacts with the ways of action. actions that an object can receive are called methods.
- Data type in C #value type variables can be assigned a value directly. they are inherited from the system.valuetype class.
- Convert data types in C #converting data types in c # is to convert one data type to another. it is also known as type injection.
- Array (Array) in C #an array stores a set of fixed-size elements in the same type. an array is used to store a data set, but it is often more useful to think of an array as a set of variables of the same type stored in adjacent memory locations.
- String (String) in C #in c #, you can use strings (strings) as array of characters. however, it is more common to use string keywords to declare a string variable. the string keyword is an alias for the system.string class in c #.
- Structure (Struct) in C #in c #, a structure is a data type. it helps you create a single variable that keeps relevant data of diverse data types. the keyword struct in c # is used to create a structure.