Exactly ! I was trying to compare the IL for both the debug and the release
builds of the code that you had posted ..

 class Class1
 {
  static void Main(string[] args)
  {
   if( args.Length == 0 )
   {
    Console.WriteLine("in if");
    try
    {
    }
    finally
    {
    }
   }
   else
   {
    Console.WriteLine("in else");
   }
  }
 }

Now the IL suggests this when optimisation is turned ON in release mode and
debug mode !

Release:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       26 (0x1a)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  ldlen
  IL_0002:  conv.i4
  IL_0003:  brtrue.s   IL_000f
  IL_0005:  ldstr      "in if"
  IL_000a:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000f:  ldstr      "in else"
  IL_0014:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0019:  ret
} // end of method Class1::Main


Debug

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       31 (0x1f)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  ldlen
  IL_0002:  conv.i4
  IL_0003:  brtrue.s   IL_0014
  IL_0005:  ldstr      "in if"
  IL_000a:  call       void [mscorlib]System.Console::WriteLine(string)
  .try
  {
    IL_000f:  leave.s    IL_0012
  }  // end .try
  finally
  {
    IL_0011:  endfinally
  }  // end handler
  IL_0012:  br.s       IL_001e
  IL_0014:  ldstr      "in else"
  IL_0019:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_001e:  ret
} // end of method Class1::Main

If you see, the optimizer in the release mode has removed the try-catch
block for need to improve speed and to avoid extra IL instructions which do
not have any need or importance since they dont contain any code. Now i
think the compiler has also removed the else block along with the try-catch
and so what has happenned is that the code now is like this .

 class Class1
 {
  static void Main(string[] args)
  {
   if( args.Length == 0 )
   {
    Console.WriteLine("in if");
   }
   Console.WriteLine("in else");
  }
 }

That explains the problem that shows up as saying, when there are no args,
both "in if" and "in else" show up while if there are then only "in else"
shows up !

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
NEW! ASP.NET courses you may be interested in:

2 Days of ASP.NET, 29 Sept 2003, in Redmond
http://www.develop.com/courses/2daspdotnet

Guerrilla ASP.NET, 13 Oct 2003, in Boston
http://www.develop.com/courses/gaspdotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to