Multiple Main Method in C#

In this article we will know how to handle a program with more than one entry point. All of us know the basic programming concept that is one program allow only and only one entry point i.e A Main Method. This is what normally expected. However, C# also allows you to create a program with one Entrance point its means one Main method.

but it will be very sound interesting to have more than one entry point in program. yeah! we can with illegal way and Infect with weird way. have a read below article to explore more.. 🙂

What is Method :-

Method or in other word we can also say that function is a collection of statement, rules embedded with its access specifier, return type and parameter having unique name resides in a class. A method or function can be Static or non static Function

What is Main Method

When a program starts, it looks for an entry point. This is the role of the Main() method. In fact, a program, that is an executable program, starts by, and stops with, the Main() method. The way this works is that, at the beginning, the compiler looks for a method called Main. If it doesn’t find it, it produces an error.

Main Method is the entry point for program execution. Hence we can say that without  Main Method we cannot imagine for a successful execution of a program.

Code Use  for exploring this article

using System;
using System.Collections.Generic;
using System.Text;

namespace Multiple_MainClasses
    class A
        static void Main(string[] args)
            Console.WriteLine("I am from Class A");
    class B
        static void Main(string[] args)

            Console.WriteLine("I am from Class B");

What happen when we Execute This Code 

  • By using Ctrl+F5 and F5of Visual Studio IDE.
  • By using Visual Studio CMD Prompt(visual Stdudio Command Prompt) .

When we execute this code by using any method we will get error and the execution will be failed.

Lets us look what is error and what is solution to resolve the problem and execute the given code.

When using Visual Studio Editor

When we use this program in Visual Studio Editor and try to execute this program we will get error like given below

Error :- There were build errors. Would you  like to continue and run the last successful build?

According to your action :-

When you will click on Yes Button, A Warning will appear like

And if you will press No button then error( What is error in the Program) Will be Shown like given below:- 
What is Error in This Code

now  after taking three snaps you knew about the error. The error is that in this program Two Entry point is resides which is not legal programmatically in fact there should be only one entry point.

During compiling this program compiler will be in confusion which is Entry Point because Compiler get two Entry Point. Hence we can assume that one address two path. Which path have to follow.

How This Problem Will Be Solved

This problem can be resolved by specifying which Main is to be used to the compiler at the time of compilation.

When you Using Visual Studio Editor

then follow the steps given in image

  • Go to solution explorer, if solution explorer is not appear then press Ctrl+Alt+L .
  • right click on projectname, eg:- MultipleMainClasses and then
  •  click on properties.

After click on Properties A new Window Appear like given below

Initially no one Method is selected in start up object  you need to select.

  • Click on Application Tab.
  • Select Class with method which you want to execute as given in fig.
Multiple Main Method - ImgAfter select start up object execute Program by pressing f5.
This is the output of Class A Main Method
Multiple Main Method - Img1and This is the output of Class A Main Method
Multiple Main Method - Img2

Hey you have done it 🙂

When you Using Visual Studio CMD Prompt

then Try to execute as given below:

csc filename.cs /main:classname

where  filename is the name of the file where the code is stored and classname is the name of the class containing the Main which we would like to be the entry point.

As in given program there are two class A and B in which each containing A Main Method. We may write as

csc filename.cs /main:A           [ for Class    A  Main Execution ]   or,      

csc filename.cs /main:B            [  for Class    B  Main Execution ]


we can create a program having more than on entry point and also learned that how to handle it. but remember keeping more than one entry point is not legal as a rule so keep avoiding keeping more than one entry point.

About Ravi Ranjan Kumar 32 Articles
An Indian who Living, Loving & Learning Technology with different tastes and willing to share knowledge and thoughts.


  1. The analysis provided the partially correct.
    while compiling the above code using command prompt you need to specify fully qualified path of the class name.hence the command will look like :
    csc filename.cs /main:Multiple_MainClasses.B

  2. Ya Its nice but if that class is not displayed in the start up object menu what should we do
    please answer me….. i am getting same error

    • Raman bhai ek saath kabhi nahi hoga. Entry point to koi ek hi na hoga. agar dono gate khul gaya to bechara compiler confuse ho jaega ki kidhar se entry maru aur phir wo warning dega isse accha hai ki socho hi aisa karo hi mat.

  3. I was really surprised when i heard about multiple main methods declarations within a single program….but Ur so well elaborated examples and explanation made me satisfied…!! Thanks for sharing such wonderful stuffs..!!

    • As I know You can only have multiple Main() method in your program but only one can be used as an entry point in that case others are meaningless. it recommended that we must inform the compiler which class need to execute as entry point while execution.

1 Trackback / Pingback

  1. Experiencia con Java pero novato con C#

Leave a Reply to gaurav Cancel reply