Code that is located after the return statement is not executed. In the 1th program given below, you will find that there is a WriteLine function call in C #, but we do not see it in our IL code. This is because the compiler realizes that any statement after the return will not be executed, and thus does not have to convert it into IL.
A.cs
class zzz
{
public static void Main()
{
return;
System.Console.WriteLine("hi");
}
}
A.il
.assembly mukhi {}
.class private auto ansi zzz extends System.Object
{
.method public hidebysig static void vijay() il managed
{
.entrypoint
br.s IL_0002
IL_0002: ret
}
}
Instead of wasting time on compiling code that is never executed, the compiler generates a warning when it encounters this situation.
A.cs
class zzz
{
public static void Main()
{
}
zzz( int i)
{
System.Console.WriteLine("hi");
}
}
A.il
.assembly mukhi {}
.class private auto ansi zzz extends System.Object
{
.method public hidebysig static void vijay() il managed
{
.entrypoint
ret
}
.method private hidebysig specialname rtspecialname instance void .ctor(int32 i) il managed
{
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
ldstr "hi"
call void [mscorlib]System.Console::WriteLine(class System.String)
ret
}
}