Share via


MSIL Trivia - 1

Background: If you have multiple Main methods in your C# console application, you will be able to compile the application only if you provide the /main:ClassName on your command prompt. For ex. create a file called SampleMSIL.cs and paste the following...

using System;
class AA
{
   public static void Main()
   {
      Console.WriteLine("In class AA");
   }
}
class BB
{
   public static void Main()
   {
      Console.WriteLine("In class BB");
   }
}

You can use csc.exe SampleMSIL.cs /main:AA or BB .

Let's say you chose AA or BB while compiling and forgot which option you chose. In our case it won't make much of a difference since the EXE is small (you can re-compile again!!). But if the build takes a long time, will there be any way to tell which class is currently the active one?

Open this SampleMSIL.exe in ILDasm.exe (IL Disassembler) , you will get something like...

image

Answer:

If you open Main:void() for both class AA and BB you will get something like this...

image

The main entry point of this Exe can be seen highlighted in the picture above. Thus in our case, when you run the EXE, you will see the output as In class BB.

Until next time Wave,
Rahul

Quote of the day:
To invent, you need a good imagination and a pile of junk. - Thomas A. Edison

Comments