A few days back while discussing some OOPS concepts one of my colleagues asked me if it is possible to have multiple main methods inside an assembly..Those who have been a die hard Visual Studio IDE fan will straight away say no to the question but if have really explored the powers of .Net command line compiler, then your answer will be "Yes You can have multiple main methods inside an assembly".
Now how's this possible?
As per books we have read...
Main is the entry point of your assembly.
Now the question arise can we have multiple entry points inside an assembly, the answer is NO. You can only have one entry point inside an assembly but it doesn't stop us from defining more than one method inside an assembly. With that you need to specify which Main to be used as entry point while compiling your application..Let's try to understand with below example
using System;
class FirstMain
{
public static void Main()
{
Console.WriteLine("FirstMain");
}
}
class SecondMain
{
public static void Main()
{
Console.WriteLine("SecondMain");
}
}
Now to compile above try below
csc FirstHelloFileName.cs /main:FirstHello Will choose Main from the FirstHello class
or
csc SecondHelloFileName.cs /main:SecondHello Will choose Main from the SecondHello class
Hi Shakti,
ReplyDeleteIn Command:
csc FirstHelloFileName.cs /main:FirstHello
Is theFirstHello the name of the class then should not it be FirstMain in the above code snippet. I tried to run this command but have not been go through yet.
My Code:
namespace ConsoleApplication
{
class FirstMainClass
{
static void Main(string[] args)
{
Console.WriteLine("Main function entry from FirstMainClass");
}
}
class SecondMainClass
{
static void Main(string[] args)
{
Console.WriteLine("Main function entry from SecondMainClass");
}
}
}
error: Could not find "FirstMainClass". This is under same file.
I am supplying my physical path in place of "FirstHelloFileName.cs" and my class name at the placve of "FirstHello"
Thanks!
Try below steps
ReplyDelete1.) Create a file C:\test.cs and add your code there.
2.) csc "C:\test.cs" /main:ConsoleApplication.FirstHello
Tried it but showing the error msg "Could not find 'ConsoleApplication.FirstMainClass'.
ReplyDeleteAm I missing any step?
Thanks!