Quantcast
Viewing all articles
Browse latest Browse all 13

switch/case with string

For me it appears to use a hashtable if there are 10 or more cases.  This is with .NET 1.1.

 

Code Snippet

switch

(s)

{

    case"str1":

        Console.WriteLine("1");

        break;

    case"str2":

        Console.WriteLine("2");

        break;

    // ((str3 to str8 not shown))

    case"str9":

        Console.WriteLine("9");

        break;

    case"str10":

        Console.WriteLine("10");

        break;

}

 

This compiles in release mode to the following IL:

.methodpublichidebysigstaticvoidDeleteMe(string s)cilmanaged
{.maxstack 4.localsinit (
        [0] objectobj2)
    L_0000: volatile
    L_0002: ldsfldclass [mscorlib]System.Collections.Hashtable<PrivateImplementationDetails>::$$method0x6000224-1
    L_0007: brtrue L_00ca
    L_000c: ldc.i4.s0x14
    L_000e: ldc.r40.5
    L_0013: newobjinstancevoid [mscorlib]System.Collections.Hashtable::.ctor(int32, float32)
    L_0018: dup
    L_0019: ldstr"str1"
    L_001e: ldc.i4.0
    L_001f: boxint32
    L_0024: callinstancevoid [mscorlib]System.Collections.Hashtable::Add(object, object)
    L_0029: dup
    L_002a: ldstr"str2"
    L_002f: ldc.i4.1
    L_0030: boxint32
    L_0035: callinstancevoid [mscorlib]System.Collections.Hashtable::Add(object, object)
    L_003a: dup  [code for "str3" to "str8" omitted]
    L_00a1: ldstr"str9"
    L_00a6: ldc.i4.8
    L_00a7: boxint32
    L_00ac: callinstancevoid [mscorlib]System.Collections.Hashtable::Add(object, object)
    L_00b1: dup
    L_00b2: ldstr"str10"
    L_00b7: ldc.i4.s0x9
    L_00b9: boxint32
    L_00be: callinstancevoid [mscorlib]System.Collections.Hashtable::Add(object, object)
    L_00c3: volatile
    L_00c5: stsfldclass [mscorlib]System.Collections.Hashtable<PrivateImplementationDetails>::$$method0x6000224-1
    L_00ca: ldarg.0
    L_00cb: dup
    L_00cc: stloc.0
    L_00cd: brfalse L_0188
    L_00d2: volatile
    L_00d4: ldsfldclass [mscorlib]System.Collections.Hashtable<PrivateImplementationDetails>::$$method0x6000224-1
    L_00d9: ldloc.0
    L_00da: callinstanceobject [mscorlib]System.Collections.Hashtable::get_Item(object)
    L_00df: dup
    L_00e0: stloc.0
    L_00e1: brfalse L_0188
    L_00e6: ldloc.0
    L_00e7: unboxint32
    L_00ec: ldind.i4
    L_00ed: switch (L_011b, L_0126, L_0131, L_013c, L_0147, L_0152, L_015d, L_0168, L_0173, L_017e)
    L_011a: ret
    L_011b: ldstr"1"
    L_0120: callvoid [mscorlib]System.Console::WriteLine(string)
    L_0125: ret
    L_0126: ldstr"2"
    L_012b: callvoid [mscorlib]System.Console::WriteLine(string)
    L_0130: ret[code for "str3" to "str8" omitted]
    L_0173: ldstr"9"
    L_0178: callvoid [mscorlib]System.Console::WriteLine(string)
    L_017d: ret
    L_017e: ldstr"10"
    L_0183: callvoid [mscorlib]System.Console::WriteLine(string)
    L_0188: ret
}

 

As you can see, it uses a jump table. It didn't do this when I removed one of the strings though.


Viewing all articles
Browse latest Browse all 13

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>