Home  >  Article  >  Backend Development  >  What is the basic structure of a C# program?

What is the basic structure of a C# program?

WBOY
WBOYforward
2023-09-27 16:33:021415browse

What is the basic structure of a C# program?

Let us first look at a C# sample program-

using System;

namespace DemoApplication {

   class HelloWorld {

      static void Main(string[] args) {

         Console.WriteLine("Welcome!");
         Console.ReadKey();
      }
   }
}

Now let us see what the above program contains-

using

The first line of the program using System; - The using keyword is used to include the system namespace in the program. A program usually has multiple using statements.

Namespace declaration

The next line is the namespace declaration. A namespace is a collection of classes. The DemoApplication namespace contains the class HelloWorld.

Class Declaration

The next line has a class declaration, class HelloWorld contains the data and method definitions used by your program. Classes usually contain multiple methods. Methods define the behavior of a class. However, the HelloWorld class has only one method Main.

Main method

The next line defines the Main method, which is the entry point program for all C#. The Main method describes what the class does when executed.

Console.WriteLine

The Main method uses the statement Console.WriteLine("Welcome!"); to specify its behavior. >

WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Welcome!"

Console.ReadKey()

The last line Console.ReadKey(); is for VS.NET users. This causes the program to wait for key presses and prevents the screen from quickly running and closing when launching the program from Visual Studio .NET.

The above is the detailed content of What is the basic structure of a C# program?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete