Skip to content

Instantly share code, notes, and snippets.

@DmitryZinchenko
Created April 6, 2016 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DmitryZinchenko/9ed5ba31b79cc3ab60c3f39419aaf35a to your computer and use it in GitHub Desktop.
Save DmitryZinchenko/9ed5ba31b79cc3ab60c3f39419aaf35a to your computer and use it in GitHub Desktop.
C# 6.0 Program and IL (Null-Conditional Operators in C# 6.0 blog post)
namespace CS6
{
class Program
{
static void Main(string[] args)
{
var car = new Car();
int currentSpeed = GetCurrentSpeed(car);
}
private static int GetCurrentSpeed(Car car)
{
return car?.Engine?.ControlUnit?.CurrentSpeed ?? 0;
}
}
}
// Type: CS6.Program
// Assembly: CS6, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 4AB19F9C-A2C4-4C0B-93BF-64E92E91575A
// Location: D:\study\b-12\CS6\CS6\bin\Debug\CS6.exe
// Sequence point data from D:\study\b-12\CS6\CS6\bin\Debug\CS6.pdb
.class private auto ansi beforefieldinit
CS6.Program
extends [mscorlib]System.Object
{
.method private hidebysig static void
Main(
string[] args
) cil managed
{
.entrypoint
.maxstack 1
.locals init (
[0] class CS6.Car car,
[1] int32 currentSpeed
)
// [6 9 - 6 10]
IL_0000: nop
// [7 13 - 7 33]
IL_0001: newobj instance void CS6.Car::.ctor()
IL_0006: stloc.0 // car
// [8 13 - 8 53]
IL_0007: ldloc.0 // car
IL_0008: call int32 CS6.Program::GetCurrentSpeed(class CS6.Car)
IL_000d: stloc.1 // currentSpeed
// [9 9 - 9 10]
IL_000e: ret
} // end of method Program::Main
.method private hidebysig static int32
GetCurrentSpeed(
class CS6.Car car
) cil managed
{
.maxstack 2
.locals init (
[0] valuetype [mscorlib]System.Nullable`1<int32> V_0,
[1] valuetype [mscorlib]System.Nullable`1<int32> V_1,
[2] int32 V_2
)
// [12 9 - 12 10]
IL_0000: nop
// [13 13 - 13 64]
IL_0001: ldarg.0 // car
IL_0002: brtrue.s IL_000f
IL_0004: ldloca.s V_1
IL_0006: initobj valuetype [mscorlib]System.Nullable`1<int32>
IL_000c: ldloc.1 // V_1
IL_000d: br.s IL_0042
IL_000f: ldarg.0 // car
IL_0010: call instance class CS6.Engine CS6.Car::get_Engine()
IL_0015: dup
IL_0016: brtrue.s IL_0024
IL_0018: pop
IL_0019: ldloca.s V_1
IL_001b: initobj valuetype [mscorlib]System.Nullable`1<int32>
IL_0021: ldloc.1 // V_1
IL_0022: br.s IL_0042
IL_0024: call instance class CS6.ControlUnit CS6.Engine::get_ControlUnit()
IL_0029: dup
IL_002a: brtrue.s IL_0038
IL_002c: pop
IL_002d: ldloca.s V_1
IL_002f: initobj valuetype [mscorlib]System.Nullable`1<int32>
IL_0035: ldloc.1 // V_1
IL_0036: br.s IL_0042
IL_0038: call instance int32 CS6.ControlUnit::get_CurrentSpeed()
IL_003d: newobj instance void valuetype [mscorlib]System.Nullable`1<int32>::.ctor(!0/*int32*/)
IL_0042: stloc.0 // V_0
IL_0043: ldloca.s V_0
IL_0045: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue()
IL_004a: brtrue.s IL_004f
IL_004c: ldc.i4.0
IL_004d: br.s IL_0056
IL_004f: ldloca.s V_0
IL_0051: call instance !0/*int32*/ valuetype [mscorlib]System.Nullable`1<int32>::GetValueOrDefault()
IL_0056: stloc.2 // V_2
IL_0057: br.s IL_0059
// [14 9 - 14 10]
IL_0059: ldloc.2 // V_2
IL_005a: ret
} // end of method Program::GetCurrentSpeed
.method public hidebysig specialname rtspecialname instance void
.ctor() cil managed
{
.maxstack 8
IL_0000: ldarg.0 // this
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method Program::.ctor
} // end of class CS6.Program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment