Why is the Main() method use in C# static?

CsharpProgrammingServer Side Programming

The Main method states what the class does when executed and instantiates other objects and variables.

A main method is static since it is available to run when the C# program starts. It is the entry point of the program and runs without even creating an instance of the class.

The following shows how to add a Main() method with static −

Example

 Live Demo

using System;

namespace Demo {
   class HelloWorld {
      static void Main(string[] args) {

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

Output

Bingo!

As you can see in the above example −

static void Main(string[] args) {

Here,

  • static − the object is not needed to access static members

  • void − return type of the method

  • Main − entry point for any C# program. Program execution begins here.

  • string[] args −  for command line arguments in C#.

raja
Published on 07-Aug-2018 12:57:04
Advertisements