Skip to content

Instantly share code, notes, and snippets.

@DmitryZinchenko
Last active April 6, 2016 03:49
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/4ab0eae97393630295a66671ccaed445 to your computer and use it in GitHub Desktop.
Save DmitryZinchenko/4ab0eae97393630295a66671ccaed445 to your computer and use it in GitHub Desktop.
C# 5.0 Program and IL (Null-Conditional Operators in C# 6.0 blog post)
namespace CS5
{
class Program
{
static void Main(string[] args)
{
var car = new Car();
int currentSpeed = GetCurrentSpeed(car);
}
private static int GetCurrentSpeed(Car car)
{
if (car != null && car.Engine != null && car.Engine.ControlUnit != null)
{
return car.Engine.ControlUnit.CurrentSpeed;
}
return 0;
}
}
}
// Type: CS5.Program
// Assembly: CS5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 44B9560D-616F-4E8B-B099-326D477FE514
// Location: D:\study\b-12\CS5\CS5\bin\Debug\CS5.exe
// Sequence point data from D:\study\b-12\CS5\CS5\bin\Debug\CS5.pdb
.class private auto ansi beforefieldinit
CS5.Program
extends [mscorlib]System.Object
{
.method private hidebysig static void
Main(
string[] args
) cil managed
{
.entrypoint
.maxstack 1
.locals init (
[0] class CS5.Car car,
[1] int32 currentSpeed
)
// [6 3 - 6 4]
IL_0000: nop
// [7 4 - 7 24]
IL_0001: newobj instance void CS5.Car::.ctor()
IL_0006: stloc.0 // car
// [8 4 - 8 44]
IL_0007: ldloc.0 // car
IL_0008: call int32 CS5.Program::GetCurrentSpeed(class CS5.Car)
IL_000d: stloc.1 // currentSpeed
// [9 3 - 9 4]
IL_000e: ret
} // end of method Program::Main
.method private hidebysig static int32
GetCurrentSpeed(
class CS5.Car car
) cil managed
{
.maxstack 2
.locals init (
[0] int32 CS$1$0000,
[1] bool CS$4$0001
)
// [12 3 - 12 4]
IL_0000: nop
// [13 4 - 13 76]
IL_0001: ldarg.0 // car
IL_0002: brfalse.s IL_001c
IL_0004: ldarg.0 // car
IL_0005: callvirt instance class CS5.Engine CS5.Car::get_Engine()
IL_000a: brfalse.s IL_001c
IL_000c: ldarg.0 // car
IL_000d: callvirt instance class CS5.Engine CS5.Car::get_Engine()
IL_0012: callvirt instance class CS5.ControlUnit CS5.Engine::get_ControlUnit()
IL_0017: ldnull
IL_0018: ceq
IL_001a: br.s IL_001d
IL_001c: ldc.i4.1
IL_001d: nop
IL_001e: stloc.1 // CS$4$0001
IL_001f: ldloc.1 // CS$4$0001
IL_0020: brtrue.s IL_0036
// [14 4 - 14 5]
IL_0022: nop
// [15 5 - 15 48]
IL_0023: ldarg.0 // car
IL_0024: callvirt instance class CS5.Engine CS5.Car::get_Engine()
IL_0029: callvirt instance class CS5.ControlUnit CS5.Engine::get_ControlUnit()
IL_002e: callvirt instance int32 CS5.ControlUnit::get_CurrentSpeed()
IL_0033: stloc.0 // CS$1$0000
IL_0034: br.s IL_003a
// [18 4 - 18 13]
IL_0036: ldc.i4.0
IL_0037: stloc.0 // CS$1$0000
IL_0038: br.s IL_003a
// [19 3 - 19 4]
IL_003a: ldloc.0 // CS$1$0000
IL_003b: 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: ret
} // end of method Program::.ctor
} // end of class CS5.Program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment