Multiple Main Methods in C#


From this you will know what is Multiple Main Methods in C#. C# enables to define more than one class with the Main method, and 'Main' is the entry points.

Learn Multiple Main Methods in C#


C# includes a feature that enables to define more than one class with the Main method. Since 'Main' is the entry point for program execution, there are now more than one entry points. In fact, there should be only one entry point. This problem can be resolved by specifying which Main is to be used to the compiler at the time of compilation as shown below:


csc filename.cs/main:classname


The following code snippet shows how to implement multiple Main methods in C#.


using System;

class ClassA
{
public static void Main()
{
Console.WriteLine("Class A");
}
}
class ClassB
{
public static void Main()
{
Console.WriteLine("Class B");
}
}


Save this file as 'Multimain.cs' and compile it using
csc Multimain.cs/main:ClassA if you choose ClassA as entry point
or
csc Multimain.cs/main:ClassB if you choose ClassB as entry point.


Comments

Guest Author: Mohd.monu03 Apr 2012

But how to call a particular Main function???????????
update it now if you can

Author: Shanthi M02 May 2012 Member Level: Gold   Points : 1

To call a particular Main function, we have to mention the main method of which class to be called at the time of compilation as shown below:

If the file name is 'Multimain.cs' and compile it using
csc Multimain.cs/main:ClassA -> calls Main method of ClassA
csc Multimain.cs/main:ClassB -> calls Main method of ClassB

Guest Author: naina21 Aug 2012

its wrong its not working

Guest Author: naina21 Aug 2012

its not working

Guest Author: naina23 Aug 2012

its useless to be there .. u try hard as u can . but u lll nt get the result ... tht i m 100% sure abut this .i rty hard ... but it will wrk on java



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: