Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 10708 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > OOPS Interview Questions > Can we have Multiple Main Methods in one ...

Can we have Multiple Main Methods in one .cs file

Interview question and answer by: Pradsir | Posted on: 2/21/2011 | Category: OOPS Interview questions | Views: 2826 | | Points: 40


Answer:

Yes we can Have multiple Main methods in one .cs file.
The crux is we can have Multiple classes in one .cs file; and we can define one Main method in each class.

& while doing compliation we can spcify the compiler to choose the Main methods from the specific class .

for ef see the code below

using System;


class Test
{
public static void Main()
{
Console.WriteLine("Test");
}
}

class Demo
{
public static void Main()
{
Console.WriteLine("Demo");
}
}
We have got two class which we can save in single .cs file say Hello.cs

while doing compliation we can say

csc Hello.cs /main:Demo        --> In order to choose Main from the Demo class

and
csc Hello.cs /main:Test --> In order to choose Main from the Test class

Happy coding..

Asked In: Many Interviews | Alert Moderator 
Found interesting? Add this to:


 Responses

Posted by: Kishork80 | Posted on: 22 Feb 2011 07:05:04 AM | Points: 10 | Alert Moderator 

We can have multiple Main Methods inside a single class. While compiling they only give warning but we can run the class successfully.
NOTE: It is mandatory to have either Main method with data type (string[]) or without data type to run i.e. no paramater.
In any case only one should be present to run the class.

Example code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("string[]");
Console.Read();
}
/*
static void Main()
{
Console.WriteLine("no param");
Console.Read();
}
*/
static void Main(string args)
{
Console.WriteLine("string");
Console.Read();
}
static void Main(int args)
{
Console.WriteLine("int");
Console.Read();
}
static void Main(double args)
{
Console.WriteLine("double");
Console.Read();
}
static void Main(float args)
{
Console.WriteLine("float");
Console.Read();
}
}
Warnings:
ConsoleApplication4.Program.Main(string)' has the wrong signature to be an entry point
ConsoleApplication4.Program.Main(int)' has the wrong signature to be an entry point
ConsoleApplication4.Program.Main(double)' has the wrong signature to be an entry point
ConsoleApplication4.Program.Main(float)' has the wrong signature to be an entry point


OUTPUT :
string[]

Posted by: Pradsir | Posted on: 22 Feb 2011 11:31:30 AM | Points: 10 | Alert Moderator 

Hi Kishor,

As per my understanding you are using VS,Please can we have try with Notepad & using Csc.exe..

also in Main methods we can not pass parameters like doble, float..

Thanks

Posted by: Pradsir | Posted on: 22 Feb 2011 11:38:43 AM | Points: 10 | Alert Moderator 

Hi

Hey 1 Good & intersting found..

Sorry kishor, I tried your code.. & found we can pass float also as parameter..

see the code below

class Program {
public static void Main(string[] args)
{

Console.WriteLine("Main Method"); Main(100.00f);
}
public static void Main(int x)
{
Console.WriteLine("Overloaded Main Method: x value is {0}", x);
}

public static void Main(float x)
{
Console.WriteLine("Overloaded Main Method: x ffffffffvalue is {0}", x);
}
}class Program {
public static void Main(string[] args)
{

Console.WriteLine("Main Method"); Main(100.00f);
}
public static void Main(int x)
{
Console.WriteLine("Overloaded Main Method: x value is {0}", x);
}

public static void Main(float x)
{
Console.WriteLine("Overloaded Main Method: ffffffffvalue is {0}", x);
}
}

>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from Pradsir

Even more ... | Submit Interview Questions and win prizes!


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 11:51:10 PM