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:

C # program structure Picture 1

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

4.5 ★ | 2 Vote

May be interested

  • Structure (Struct) in C programmingStructure (Struct) in C programming
    arrays in c allow you to define several types of variables that can hold the values ​​of several components of the same type. but the structure is another type of data in the c programming language, which allows you to combine other types of data.
  • What is URL? Structure of the URLWhat is URL? Structure of the URL
    what exactly is a url, what is its structure and composition? this article will give you an overview of urls and its structure.
  • New achievement: TSP chip structure can run 1 million billion operations per secondNew achievement: TSP chip structure can run 1 million billion operations per second
    chips with the tsp structure of startup groq can perform up to 1 peta task per second, that is, 1,000,000,000,000,000 actions per second.
  • Learn about URLsLearn about URLs
    you must have heard of the url but don't know exactly what it is, how the structure is and what the composition is. this article will therefore give you an overview of the url and its structure.
  • Bit field in CBit field in C
    suppose your c program includes a number of true / false variables grouped in a structure called a page to check if the manufactured goods have sufficient width and height allowed.
  • Heap data structureHeap data structure
    data structure the heap is a special case of a balanced binary tree data structure, where the root node's key is compared to its children and arranged accordingly.
  • Nested Structure in GolangNested Structure in Golang
    go language allows nested structures. a structure that is a field of another structure is called nested structure.
  • How to fix 'Critical Structure Corruption' blue screen error on Windows 10How to fix 'Critical Structure Corruption' blue screen error on Windows 10
    critical structure corruption is a blue screen of death (bsod) error that can be encountered at any time while you are using a windows computer.
  • Add Structure to Diagram in Visio 2010 using List and ContainerAdd Structure to Diagram in Visio 2010 using List and Container
    in the microsoft visio 2010 model design application, one of the most used features is to assign a structure - structure to the chart - diagrams using containers, lists and callouts. in the following article, we will go deeper and learn more about the above concepts and how to use them accordingly ...
  • Structure & Principle of operation of air conditioner, air conditionerStructure & Principle of operation of air conditioner, air conditioner
    join tipsmake.com to learn about the structure and operation principles of air conditioning and air conditioning - familiar devices that help us overcome the hot summer days.