Multiple Main methods in a C# programe

In this article I shall explain how to use multiple Main methods in a program. To do so we need to make some changes in settings.

using System;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine("VALUE IS=" + args[i]);
 
            }
            Console.ReadLine();
        }
    }
    class student
    {
        static void Main(string[] args)
        {
            if (args[0] == "user" && args[1] == "pass")
            {
                Console.WriteLine("welcome user");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("invalid  user");
                Console.ReadLine();
            }
        }
 
    }
    }

Description:- we  can use more than one main method in the program,But compiler is not know that which main method take as an entry point. so we have to set an entry point from the Startup Object in the Framework.
Here we have used two main method in above program, now I am going to execute them one by one
There are some steps to Run main method in above program.
Step1:- First open your visual studio->go File->New project->Select Console Application>click OK->copy the above code and paste the program.cs file.
see it:-

1

Step2:- Open Solution Explorer window-> Right click on Your project->click properties .
see it:-

2

After click on properties of main method program, window will be opened.it is like this:-

3

Step3:- Click the Debug -> Pass the value(which you want) in Command line arguments.
see it:- here i have two value “user” ,”pass”.

Step4:- Now click on Application->go Startup object->select class which main method you want to execute.
see it:-here i have selected student class ,so student class main method will be executed.

5

 

Now run your program.

 

Source: http://www.msdotnet.co.in/2012/06/how-to-use-multiple-main-method-in-c.html

 

Advertisements

2 thoughts on “Multiple Main methods in a C# programe

    1. Some times we need different-2 entry point like one for console app another for windows app. Also we use multiple main methods for testing purpose.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s