Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 6 April 2015

Can we have multiple Main methods in one class

Today in Engineer's World, we are discussing a very important topic of C# - Multiple Main Method in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS

Click imaginationhunt.blogspot to see latest Blogs


This is the most asked Interview Question. 
Can we have multiple main methods in one class?
Answer is Yes. We can use more than one Main Method in C# program by making the entry point one by one. 

To understand more clearly follow the easy steps:

Step 1: First open your Visual Studio -> Go to File Menu -> New project -> Select Console Application -> Name Your Application -> Browse the path where you have to save your project ->click OK.
Now, write the same piece of code in your program.cs file to understand it better.

Step 2: Now, as you have written the code. Press F6. 
You are going to face an error which says that " Specify the entry point to your project ". See Figure-2

This may happen because you have two Main Method. But compiler doesn't know that which main method take as an entry point. Either the Program1 or the Program2. 

Click imaginationhunt.blogspot to see latest Blogs

Step 3: So, for solving this issue what you have to do is, you have to set an entry point. 
Now, How to create entry points?
To create entry points Right Click on Solution Explorer -> Go to Properties. See Figure-3   


Here, under Startup object: You see that both the classes Program1 and Program2 are present. Select object for Program1. And Press Ctrl+S(Save)

Step 4: Now, Press F6. And check the status bar it is showing Build Succeeded. Now Debug the Program by pressing Ctrl+F5. Figure-5 shows you the output.


Step 5: Again go to properties and this time select Program2 object in the Startup Project. Save it(Ctrl+S). 

Press F6. Build Succeeded. Now Debug the Program by pressing Ctrl+F5. Figure-7 shows you the output for Program2 method.

Hence, by this way we can have multiple Main methods in one class.

To read Decision making C# concept - Click here.
Related Questions:-

#newtoprgm try first program

Q-1 Does Startup project may contain only Main method class object or they may contain any method class object that is going to use in this program?
Ans- For this we have created a new class i.e. Program3, which contain method Path(). Saving it(Ctrl+S)

Go to Properties -> Startup project. Check whether the class Program3 is created. No. You can find only two classes which contain Main Method() one is the Program1 and the other is the Program2.

Hence, we can say that Startup project contain only Main method() class object.

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...

No comments:

Post a comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...