C# Coalesce Exploration
Recording some exploration notes on the C# Coalesce operator (??) and also trying out Leo Vildosola's Code Snippet plugin for Windows Live Writer.
1: static private void Coalesce() 2: { 3: object obj1 = null; 4: object obj2 = obj1 ?? string.Empty; 5: } |
1: .method private hidebysig static void Coalesce() cil managed 2: { 3: .maxstack 2 4: .locals init ( 5: [0] object obj1, 6: [1] object obj2) 7: L_0000: nop 8: L_0001: ldnull 9: L_0002: stloc.0 10: L_0003: ldloc.0 11: L_0004: dup 12: L_0005: brtrue.s L_000d 13: L_0007: pop 14: L_0008: ldsfld string [mscorlib]System.String::Empty 15: L_000d: stloc.1 16: L_000e: ret 17: } 18: |
1: static private void ManualCoalesce() 2: { 3: object obj1 = null; 4: object obj2 = obj1 != null ? obj1 : string.Empty; 5: } |
1: .method private hidebysig static void ManualCoalesce() cil managed 2: { 3: .maxstack 2 4: .locals init ( 5: [0] object obj1, 6: [1] object obj2) 7: L_0000: nop 8: L_0001: ldnull 9: L_0002: stloc.0 10: L_0003: ldloc.0 11: L_0004: brtrue.s L_000d 12: L_0006: ldsfld string [mscorlib]System.String::Empty 13: L_000b: br.s L_000e 14: L_000d: ldloc.0 15: L_000e: stloc.1 16: L_000f: ret 17: } 18: 19: |