C # program structure

Before learning how to build C # programming language blocks, we should explore the basic 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:

  1. Declaring Namespace
  2. A class
  3. Class method
  4. Properties of the class
  5. A Main method
  6. Commands and expressions
  7. 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:

Picture 1 of C # program structure

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:

  1. C # is case sensitive.
  2. All commands and expressions must end with a semicolon (;).
  3. The execution of the program starts at the Main method.
  4. 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:

  1. Open Visual Studio.
  2. On the menu bar, select File -> New -> Project .
  3. Select Visual C # from the Template, and then select Windows.
  4. Select Console Application.
  5. Specify a name for the project and click the OK button.
  6. This creates New Project in Solution Explorer.
  7. Write code in the Code Editor.
  8. 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:

  1. Open a Text Editor and add the above code.
  2. Save the file as helloworld.cs
  3. Open the Command Prompt tool and go to the folder where you saved the file.
  4. Compose csc helloworld.cs and press Enter to compile your code.
  5. 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).
  6. 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

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile