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) {

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

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

using

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.

Console.WriteLine

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.

Console.ReadKey()

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

Updated on: 20-Jun-2020

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements