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