Home   Cover Cover Cover Cover
 

Switch-Anweisung

A03.cs
using System;

class Test {
  
  static int DaysOf(int month) {
    int days;
    switch (month) {
      case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        days = 31; break;
      case 4: case 6: case 9: case 11:
        days = 30; break;
      case 2:
        days = 28; break;
      default:
        days = 0; break;
    }
    return days;
  }
  
  static int DaysOf1(string month) {
    int days;
    switch (month) {
      case "Jan": case "Mar": case "May": case "Jul": case "Aug": case "Oct": case "Dec":
        days = 31; break;
      case "Apr": case "Jun": case "Sep": case "Nov":
        days = 30; break;
      case "Feb":
        days = 28; break;
      default:
        days = 0; break;
    }
    return days;
  }
  
  public static void Main() {
    Console.WriteLine(DaysOf(7));
    Console.WriteLine(DaysOf1("Oct"));
  }
}

CIL-Code von DaysOf:

.method private hidebysig static int32  DaysOf(int32 month) cil managed
{
  // Code size       85 (0x55)
  .maxstack  2
  .locals init (int32 V_0,
           int32 V_1,
           int32 V_2)
  IL_0000:  ldarg.0
  IL_0001:  stloc.2
  IL_0002:  ldloc.2
  IL_0003:  ldc.i4.1
  IL_0004:  sub
  IL_0005:  switch     (
                        IL_003c,
                        IL_0046,
                        IL_003c,
                        IL_0041,
                        IL_003c,
                        IL_0041,
                        IL_003c,
                        IL_003c,
                        IL_0041,
                        IL_003c,
                        IL_0041,
                        IL_003c)
  IL_003a:  br.s       IL_004b
  IL_003c:  ldc.i4.s   31
  IL_003e:  stloc.0
  IL_003f:  br.s       IL_004f
  IL_0041:  ldc.i4.s   30
  IL_0043:  stloc.0
  IL_0044:  br.s       IL_004f
  IL_0046:  ldc.i4.s   28
  IL_0048:  stloc.0
  IL_0049:  br.s       IL_004f
  IL_004b:  ldc.i4.0
  IL_004c:  stloc.0
  IL_004d:  br.s       IL_004f
  IL_004f:  ldloc.0
  IL_0050:  stloc.1
  IL_0051:  br.s       IL_0053
  IL_0053:  ldloc.1
  IL_0054:  ret
} // end of method Test::DaysOf

CIL-Code von DaysOf1:

.method private hidebysig static int32  DaysOf1(string month) cil managed
{
  // Code size       347 (0x15b)
  .maxstack  4
  .locals init (int32 V_0,
           int32 V_1,
           object V_2)
  IL_0000:  volatile.
  IL_0002:  ldsfld     class [mscorlib]System.Collections.Hashtable ''::'$$method0x6000002-1'
  IL_0007:  brtrue     IL_00ee
  IL_000c:  ldc.i4.s   26
  IL_000e:  ldc.r4     0.5
  IL_0013:  newobj     instance void [mscorlib]System.Collections.Hashtable::.ctor(int32,
                                                                                   float32)
  IL_0018:  dup
  IL_0019:  ldstr      "Jan"
  IL_001e:  ldc.i4.0
  IL_001f:  box        [mscorlib]System.Int32
  IL_0024:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_0029:  dup
  IL_002a:  ldstr      "Mar"
  IL_002f:  ldc.i4.1
  IL_0030:  box        [mscorlib]System.Int32
  IL_0035:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_003a:  dup
  IL_003b:  ldstr      "May"
  IL_0040:  ldc.i4.2
  IL_0041:  box        [mscorlib]System.Int32
  IL_0046:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_004b:  dup
  IL_004c:  ldstr      "Jul"
  IL_0051:  ldc.i4.3
  IL_0052:  box        [mscorlib]System.Int32
  IL_0057:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_005c:  dup
  IL_005d:  ldstr      "Aug"
  IL_0062:  ldc.i4.4
  IL_0063:  box        [mscorlib]System.Int32
  IL_0068:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_006d:  dup
  IL_006e:  ldstr      "Oct"
  IL_0073:  ldc.i4.5
  IL_0074:  box        [mscorlib]System.Int32
  IL_0079:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_007e:  dup
  IL_007f:  ldstr      "Dec"
  IL_0084:  ldc.i4.6
  IL_0085:  box        [mscorlib]System.Int32
  IL_008a:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_008f:  dup
  IL_0090:  ldstr      "Apr"
  IL_0095:  ldc.i4.7
  IL_0096:  box        [mscorlib]System.Int32
  IL_009b:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_00a0:  dup
  IL_00a1:  ldstr      "Jun"
  IL_00a6:  ldc.i4.8
  IL_00a7:  box        [mscorlib]System.Int32
  IL_00ac:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_00b1:  dup
  IL_00b2:  ldstr      "Sep"
  IL_00b7:  ldc.i4.s   9
  IL_00b9:  box        [mscorlib]System.Int32
  IL_00be:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_00c3:  dup
  IL_00c4:  ldstr      "Nov"
  IL_00c9:  ldc.i4.s   10
  IL_00cb:  box        [mscorlib]System.Int32
  IL_00d0:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_00d5:  dup
  IL_00d6:  ldstr      "Feb"
  IL_00db:  ldc.i4.s   11
  IL_00dd:  box        [mscorlib]System.Int32
  IL_00e2:  call       instance void [mscorlib]System.Collections.Hashtable::Add(object,
                                                                                 object)
  IL_00e7:  volatile.
  IL_00e9:  stsfld     class [mscorlib]System.Collections.Hashtable ''::'$$method0x6000002-1'
  IL_00ee:  ldarg.0
  IL_00ef:  dup
  IL_00f0:  stloc.2
  IL_00f1:  brfalse.s  IL_0151
  IL_00f3:  volatile.
  IL_00f5:  ldsfld     class [mscorlib]System.Collections.Hashtable ''::'$$method0x6000002-1'
  IL_00fa:  ldloc.2
  IL_00fb:  call       instance object [mscorlib]System.Collections.Hashtable::get_Item(object)
  IL_0100:  dup
  IL_0101:  stloc.2
  IL_0102:  brfalse.s  IL_0151
  IL_0104:  ldloc.2
  IL_0105:  unbox      [mscorlib]System.Int32
  IL_010a:  ldind.i4
  IL_010b:  switch     (
                        IL_0142,
                        IL_0142,
                        IL_0142,
                        IL_0142,
                        IL_0142,
                        IL_0142,
                        IL_0142,
                        IL_0147,
                        IL_0147,
                        IL_0147,
                        IL_0147,
                        IL_014c)
  IL_0140:  br.s       IL_0151
  IL_0142:  ldc.i4.s   31
  IL_0144:  stloc.0
  IL_0145:  br.s       IL_0155
  IL_0147:  ldc.i4.s   30
  IL_0149:  stloc.0
  IL_014a:  br.s       IL_0155
  IL_014c:  ldc.i4.s   28
  IL_014e:  stloc.0
  IL_014f:  br.s       IL_0155
  IL_0151:  ldc.i4.0
  IL_0152:  stloc.0
  IL_0153:  br.s       IL_0155
  IL_0155:  ldloc.0
  IL_0156:  stloc.1
  IL_0157:  br.s       IL_0159
  IL_0159:  ldloc.1
  IL_015a:  ret
} // end of method Test::DaysOf1