I came across this interesting tip in the book I'm currently reading - Essential .NET (Volume 1) by Don Box:
Assume I have the following piece of C# code:
using System; class CMain1{ public static void Main() { Console.WriteLine("hello world from 1"); } } class CMain2{ public static void Main() { Console.WriteLine("hello world from 2"); } }
using System;
class CMain1{ public static void Main() { Console.WriteLine("hello world from 1"); }
}
class CMain2{ public static void Main() { Console.WriteLine("hello world from 2"); }
When you compile this file (say hello.cs):
D:\Temp>csc hello1.csMicrosoft (R) Visual C# .NET Compiler version 7.10.3052.4for Microsoft (R) .NET Framework version 1.1.4322Copyright (C) Microsoft Corporation 2001-2002. All rights reserved. hello1.cs(5,28): error CS0017: Program 'd:\temp\hello1.exe' has more than one entry point defined: 'CMain1.Main()'hello1.cs(14,28): error CS0017: Program 'd:\temp\hello1.exe' has more than one entry point defined: 'CMain2.Main()'
D:\Temp>csc hello1.csMicrosoft (R) Visual C# .NET Compiler version 7.10.3052.4for Microsoft (R) .NET Framework version 1.1.4322Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
hello1.cs(5,28): error CS0017: Program 'd:\temp\hello1.exe' has more than one entry point defined: 'CMain1.Main()'hello1.cs(14,28): error CS0017: Program 'd:\temp\hello1.exe' has more than one entry point defined: 'CMain2.Main()'
Obviously since there are two entrypoints in the file, so the compiler wouldn't know how to compile.
The tip I learnt from the book was a compiler option that I hadn't explored (or had skipped):
The csc (and vbc) compilers have a /main: option (/m: for short) in which you can specify the type that will be the entrypoint.
So if I wanted CMain1 in the earlier example to be the entrypoint, I need to compile with the option set as:
D:\Temp>csc hello1.cs /m:CMain1Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4for Microsoft (R) .NET Framework version 1.1.4322Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
This would ensure that the ouput says hello world from 1.
It could be quite useful an option if you thought of a huge number of classes that were written by independent developers and can had used stub code for testing purposes. So instead of going and deleting all the Main methods they might have introduced, you just compile by specifying the actual Main method.
Remember Me
Page rendered at Sunday, March 14, 2010 6:34:37 AM (India Standard Time, UTC+05:30)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent the views of Microsoft Corporation in anyway.