When do you use scope without a statement in C#?

前端 未结 5 912
情深已故
情深已故 2021-01-17 07:26

Just recently I found out you can do this in C#:

{
    // google
    string url = \"#\";

    if ( value > 5 )
        url = \"http://google.com\";

    m         


        
5条回答
  •  一生所求
    2021-01-17 08:15

    One use that I find acceptable in many cases, is to enclose each switch section of a switch statement in a local scope.


    Late addition:

    The local scope blocks { ... } present in the C# source do not seem to be relevant for the resulting IL bytecode. I tried this simple example:

    static void A()
    {
        {
            var o = new object();
            Console.WriteLine(o);
        }
    
        var p = new object();
        Console.WriteLine(p);
    }
    
    static void B()
    {
        var o = new object();
        Console.WriteLine(o);
    
        var p = new object();
        Console.WriteLine(p);
    }
    
    
    static void C()
    {
        {
            var o = new object();
            Console.WriteLine(o);
        }
    
        {
            var o = new object();
            Console.WriteLine(o);
        }
    }
    

    This was compiled in Release mode (optimizations enabled). The resulting IL according to IL DASM is:

    .method private hidebysig static void  A() cil managed
    {
      // Code size       25 (0x19)
      .maxstack  1
      .locals init ([0] object o,
               [1] object p)
      IL_0000:  newobj     instance void [mscorlib]System.Object::.ctor()
      IL_0005:  stloc.0
      IL_0006:  ldloc.0
      IL_0007:  call       void [mscorlib]System.Console::WriteLine(object)
      IL_000c:  newobj     instance void [mscorlib]System.Object::.ctor()
      IL_0011:  stloc.1
      IL_0012:  ldloc.1
      IL_0013:  call       void [mscorlib]System.Console::WriteLine(object)
      IL_0018:  ret
    } // end of method LocalScopeExamples::A
    

     

    .method private hidebysig static void  B() cil managed
    {
      // Code size       25 (0x19)
      .maxstack  1
      .locals init ([0] object o,
               [1] object p)
      IL_0000:  newobj     instance void [mscorlib]System.Object::.ctor()
      IL_0005:  stloc.0
      IL_0006:  ldloc.0
      IL_0007:  call       void [mscorlib]System.Console::WriteLine(object)
      IL_000c:  newobj     instance void [mscorlib]System.Object::.ctor()
      IL_0011:  stloc.1
      IL_0012:  ldloc.1
      IL_0013:  call       void [mscorlib]System.Console::WriteLine(object)
      IL_0018:  ret
    } // end of method LocalScopeExamples::B
    

     

    .method private hidebysig static void  C() cil managed
    {
      // Code size       25 (0x19)
      .maxstack  1
      .locals init ([0] object o,
               [1] object V_1)
      IL_0000:  newobj     instance void [mscorlib]System.Object::.ctor()
      IL_0005:  stloc.0
      IL_0006:  ldloc.0
      IL_0007:  call       void [mscorlib]System.Console::WriteLine(object)
      IL_000c:  newobj     instance void [mscorlib]System.Object::.ctor()
      IL_0011:  stloc.1
      IL_0012:  ldloc.1
      IL_0013:  call       void [mscorlib]System.Console::WriteLine(object)
      IL_0018:  ret
    } // end of method LocalScopeExamples::C
    

    Conclusions:

    • Local scope declarations do not exist in any form in the IL bytecode.
    • The C# compiler will choose new names for local variables that would otherwise be in conflict (the second of the variables o in the C method).
    • People who feel (see other answers and comments to question) that the garbage collector might be able to do its work earlier when they introduce local scope in the C# source, are wrong.

    I also tried compiling this in Debug mode (no optimizations). The start and end of a local scope seem to show up as a nop instruction only ("No operation"). In some cases two identically named local variables from distinct local scopes were mapped to the same local variable in the IL, like with C# method named C above. Such a "unification" of two variables is only possible if their types are compatible.

提交回复
热议问题