Updated : Dec 31, 2021 in Articles

Main method in c#

 Main () is an entry point and also exists a point of an application, in .Net Compiler will find the first Main () method of application and it will execute code from here first.

Structure of Main () Method in C#:

Class Program
    {
Public Static void Main (string [] args)
        {
            //Statements;
        }
    }

What is void and Static Keyword?

The man method () have the void key word in the above program its means the Main() method doesn’t return any value.

C# Main () method declared with static because it can’t be called in another method of function

The compiler will be called the Main () method without creating the instantiation of Class.

The static method, static data we will call without creating the instantiation for class.

What is string[] args in C#?

If we want to pass the values to the Main () method through the command line or run time we will use string [] args.

The main () method we can accept n number of array type parameters during the runtime.

Main () Method without Command line arguments:

Class Program
    {
        Public Static void Main ()
        {
            //Statements;
        }
    }

The above Main() method parameters are optional this also eligible start-up method().

What is public access specifier keyword in Main() Method?

The public is a keyword it means the Main() method you can access anywhere in your application.

In c# we have different types of access specifiers like publicprivateprotectedinternal…etc.

Main() method without Access specifier?

Class Program
    {
       Static void Main ()
        {
            //Statements;
        }
    }

The above Main () method also eligible for executing statements without using a public keyword. If you mention the public keyword by default it is private.

Example:

Go to visual studio create a console Application enter the following given Source code examples.

Basic programming Techniques in C#:

compiling and Running The Application:

Compile:

Click on Build menuàBuild solution. (Or) ctrl+shift+B

Run:

Click on debug menuàstart debugging (or) press F5.

 With public Keyword Main() Method

namespace sampleApp
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
 
Output: Hello World!

Without public Keyword Main() Method

using System;
 
namespace sampleApp
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Hello World!");
        }
    }
}
 
Output: Hello World!

Without String[] args Main() Method

using System;
 
namespace sampleApp
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Hello World!");
        }
    }
}
 
Output: Hello World!

Passing command line arguments to String[] args to Main() Method

Go your project .sln right click go to properties =>click on debug enter the command line

namespace sampleApp
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(args[0]);
        }
    }
}
 
Output: name

Main() Method with return type

namespace sampleApp
{
    class Program
    {
        public static int Main(string[] args)
        {
          Console.WriteLine("Hello World!");
 
          return 1;
        }
    }
}
 
Output: Hello world

Latest posts by DuttaluruVijayakumar (see all)
Like
Like Love Haha Wow Sad Angry

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x