Can we have Multiple Main Methods in one .cs file

 Posted by Pradsir on 2/21/2011 | Category: OOPS Interview questions | Views: 5959 | 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 

Comments or Responses

Posted by: Kishork80 on: 2/22/2011 | Points: 10
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 on: 2/22/2011 | Points: 10
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 on: 2/22/2011 | Points: 10
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);
}
}

Login to post response