C# tutorials

Updated : Oct 13, 2020 in Articles

What is main method in c#

​in this article, we will learn what is the main method in c# and how many types of main methods in c#, and what is valid enter point of application.

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 public, private, protected, internal…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

Multiple main() method in c#

Let us suppose in your application if you have more than one main () method

Error:

I will show you compile error like you have more than one entry point in your application set which is the entry point. Run the given below source code in your application.

using System;

namespace sampleApp
{
    class program1
    {
        public static void Main()
        {
            Console.WriteLine("Main method-1");
        }
    }
    class Program2
    {
        public static void Main()
        {
            Console.WriteLine("Main method-2");
        }
    }
}

//compile error


Solution:

Go to your project =>Right click on solution select properties and click on application select startup an object which one you want to be a Main() method.

Output: Main method-1

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