What is the basic structure of a C# program?

Let us first see a sample program in C# −

using System;

namespace DemoApplication {

   class HelloWorld {

      static void Main(string[] args) {


Now let us see what all is included in the above program −


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

Namespace Declaration

The next line has 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, the class HelloWorld contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the HelloWorld class has only one method Main.

Main Method

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


The Main method specifies its behavior with the statement Console.WriteLine("Welcome!");

WriteLine is a method of the Console class defined in the Systemnamespace. This statement causes the message "Welcome!" to be displayed on the screen.


The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know