c# enables us to define more than one class with the method. Main method is the entry point for program execution. So why do we want to have more than one place for program execution. What is the advantage of multiple main methods over single main method.
Edit:
Example.cs
Class Example_1
{
public static void Main()
{
System.Console.WriteLine("Example 1")
}
public void test()
{
System.Console.WriteLine("Test method")
}
}
Class Example_2
{
public static void Main()
{
System.Console.WriteLine("Example 2")
}
}
If I type "csc Example.cs" then what would happen ? What to do if I want to inherit test method of Class Example_1 in Class Example_2. Will this code work.
Example_1 abc = new Example_1();
abc.t开发者_JAVA技巧est();
You could use it so that different build configurations built the same executable but with different entry points - for example a console entry point vs a WinForms entry point.
Personally I use it when giving talks and in the sample code for C# in Depth. Each file is a self-contained example, but it's simpler to just have one entry point - so that entry point uses a utility class to prompt the user for which example they want to run.
First, if you have multiple main methods, you can specify which to use as the entry point for the application by your compiler.
Multiple main methods can be useful for testing purposes. Maybe you're developing a windows application like a text editor. If you are working on something like syntax highlighting, for example, you can have an extra main method which starts the GUI, loads a test file and enables the appropriate syntax highlighting. Another example can be an application which will be compiled for console use only or as an windows application involving two different classes with main methods.
There can only be one entry point in a C# program. If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point.
Compile t2.cs and t3.cs, specifying that the Main method will be found in Test2 like this,
csc t2.cs t3.cs /main:Test2
You can have multiple methods called Main() in different classes. But only one of them is used to start the program. It is just normal method, you can use the other Main's for whatever you wish. (But it is not recommended.)
It would probably be a rare occurrence, but I'd imagine if you had to link to some external code that has a Main() method, then you'd want to tell the compiler to use yours.
Why shouldn't you be able to create multiple classes with a Main method? After all, Main
is just the name of the method. If you create a class it's perfectly ok to define a method named Main
in it.
When you Using Visual Studio CMD Prompt
then Try to execute as given below:
csc filename.cs /main:classname
where filename is the name of the file where the code is stored and classname is the name of the class containing the Main which we would like to be the entry point.
As in given program there are two class A and B in which each containing A Main Method. We may write as
csc filename.cs /main:A [ for Class A Main Execution ] or,
csc filename.cs /main:B [ for Class B Main Execution ]
精彩评论