Just recently I found out you can do this in C#:
{
// google
string url = \"#\";
if ( value > 5 )
url = \"http://google.com\";
m
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:
o
in the C
method).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.