How to Use Multiple Main Method In C# Program

By
We can use more than on Main Method in C# program, But there will only one Main Method which will act as entry point for the program.we can pass the value to the main method while running the program by specifying string array type parameter.
Example:-


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:-


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

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

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.


Step4:-  After execute the program you will show OUTPUT  which is shown as below:-
see it:-

Step5:-  when we select class Program Form Startup object for same Command line arguments.then OUTPUT will be:-
see it:-

Note:- you can pass different argument and execute the program.
For More:-

  1. Constructor and Destructor 
  2. Page Directives and Page Properties
  3. Data Integrity
  4. oops
  5. Partial Class

I hope this is helpful for you.
Click below for download whole application
           DOWNLOAD

2 comments:

Powered by Blogger.