Skip to content

Instantly share code, notes, and snippets.

@dresswithpockets
Created August 12, 2021 14:56
Show Gist options
  • Select an option

  • Save dresswithpockets/61082bce33287ac9a4ba86ce49c6be28 to your computer and use it in GitHub Desktop.

Select an option

Save dresswithpockets/61082bce33287ac9a4ba86ce49c6be28 to your computer and use it in GitHub Desktop.
differencec between discard and no discard in IL
using System.IO;
using System.Threading.Tasks;
public static class C {
public static async Task A() {
await File.ReadAllTextAsync("SomeFile.txt");
}
public static async Task B() {
_ = await File.ReadAllTextAsync("SomeFile.txt");
}
}
.class private auto ansi '<Module>'
{
} // end of class <Module>
.class public auto ansi abstract sealed beforefieldinit C
extends [System.Private.CoreLib]System.Object
{
// Nested Types
.class nested private auto ansi sealed beforefieldinit '<A>d__0'
extends [System.Private.CoreLib]System.ValueType
implements [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine
{
.custom instance void [System.Private.CoreLib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Fields
.field public int32 '<>1__state'
.field public valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder '<>t__builder'
.field private valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string> '<>u__1'
// Methods
.method private final hidebysig newslot virtual
instance void MoveNext () cil managed
{
.override method instance void [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext()
// Method begins at RVA 0x20c8
// Code size 157 (0x9d)
.maxstack 3
.locals init (
[0] int32,
[1] valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string>,
[2] valuetype [System.Private.CoreLib]System.Threading.CancellationToken,
[3] class [System.Private.CoreLib]System.Exception
)
// sequence point: hidden
IL_0000: ldarg.0
IL_0001: ldfld int32 C/'<A>d__0'::'<>1__state'
IL_0006: stloc.0
.try
{
// sequence point: hidden
IL_0007: ldloc.0
IL_0008: brfalse.s IL_004c
IL_000a: ldstr "SomeFile.txt"
IL_000f: ldloca.s 2
IL_0011: initobj [System.Private.CoreLib]System.Threading.CancellationToken
IL_0017: ldloc.2
IL_0018: call class [System.Private.CoreLib]System.Threading.Tasks.Task`1<string> [System.IO.FileSystem]System.IO.File::ReadAllTextAsync(string, valuetype [System.Private.CoreLib]System.Threading.CancellationToken)
IL_001d: callvirt instance valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<!0> class [System.Private.CoreLib]System.Threading.Tasks.Task`1<string>::GetAwaiter()
IL_0022: stloc.1
// sequence point: hidden
IL_0023: ldloca.s 1
IL_0025: call instance bool valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string>::get_IsCompleted()
IL_002a: brtrue.s IL_0068
IL_002c: ldarg.0
IL_002d: ldc.i4.0
IL_002e: dup
IL_002f: stloc.0
IL_0030: stfld int32 C/'<A>d__0'::'<>1__state'
IL_0035: ldarg.0
IL_0036: ldloc.1
IL_0037: stfld valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string> C/'<A>d__0'::'<>u__1'
IL_003c: ldarg.0
IL_003d: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<A>d__0'::'<>t__builder'
IL_0042: ldloca.s 1
IL_0044: ldarg.0
IL_0045: call instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::AwaitUnsafeOnCompleted<valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string>, valuetype C/'<A>d__0'>(!!0&, !!1&)
IL_004a: leave.s IL_009c
IL_004c: ldarg.0
IL_004d: ldfld valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string> C/'<A>d__0'::'<>u__1'
IL_0052: stloc.1
IL_0053: ldarg.0
IL_0054: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string> C/'<A>d__0'::'<>u__1'
IL_0059: initobj valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string>
IL_005f: ldarg.0
IL_0060: ldc.i4.m1
IL_0061: dup
IL_0062: stloc.0
IL_0063: stfld int32 C/'<A>d__0'::'<>1__state'
IL_0068: ldloca.s 1
IL_006a: call instance !0 valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string>::GetResult()
IL_006f: pop
IL_0070: leave.s IL_0089
} // end .try
catch [System.Private.CoreLib]System.Exception
{
// sequence point: hidden
IL_0072: stloc.3
IL_0073: ldarg.0
IL_0074: ldc.i4.s -2
IL_0076: stfld int32 C/'<A>d__0'::'<>1__state'
IL_007b: ldarg.0
IL_007c: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<A>d__0'::'<>t__builder'
IL_0081: ldloc.3
IL_0082: call instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::SetException(class [System.Private.CoreLib]System.Exception)
IL_0087: leave.s IL_009c
} // end handler
IL_0089: ldarg.0
IL_008a: ldc.i4.s -2
IL_008c: stfld int32 C/'<A>d__0'::'<>1__state'
// sequence point: hidden
IL_0091: ldarg.0
IL_0092: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<A>d__0'::'<>t__builder'
IL_0097: call instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::SetResult()
IL_009c: ret
} // end of method '<A>d__0'::MoveNext
.method private final hidebysig newslot virtual
instance void SetStateMachine (
class [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine stateMachine
) cil managed
{
.custom instance void [System.Private.CoreLib]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = (
01 00 00 00
)
.override method instance void [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine::SetStateMachine(class [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine)
// Method begins at RVA 0x2184
// Code size 13 (0xd)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<A>d__0'::'<>t__builder'
IL_0006: ldarg.1
IL_0007: call instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::SetStateMachine(class [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine)
IL_000c: ret
} // end of method '<A>d__0'::SetStateMachine
} // end of class <A>d__0
.class nested private auto ansi sealed beforefieldinit '<B>d__1'
extends [System.Private.CoreLib]System.ValueType
implements [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine
{
.custom instance void [System.Private.CoreLib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Fields
.field public int32 '<>1__state'
.field public valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder '<>t__builder'
.field private valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string> '<>u__1'
// Methods
.method private final hidebysig newslot virtual
instance void MoveNext () cil managed
{
.override method instance void [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext()
// Method begins at RVA 0x2194
// Code size 157 (0x9d)
.maxstack 3
.locals init (
[0] int32,
[1] valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string>,
[2] valuetype [System.Private.CoreLib]System.Threading.CancellationToken,
[3] class [System.Private.CoreLib]System.Exception
)
// sequence point: hidden
IL_0000: ldarg.0
IL_0001: ldfld int32 C/'<B>d__1'::'<>1__state'
IL_0006: stloc.0
.try
{
// sequence point: hidden
IL_0007: ldloc.0
IL_0008: brfalse.s IL_004c
IL_000a: ldstr "SomeFile.txt"
IL_000f: ldloca.s 2
IL_0011: initobj [System.Private.CoreLib]System.Threading.CancellationToken
IL_0017: ldloc.2
IL_0018: call class [System.Private.CoreLib]System.Threading.Tasks.Task`1<string> [System.IO.FileSystem]System.IO.File::ReadAllTextAsync(string, valuetype [System.Private.CoreLib]System.Threading.CancellationToken)
IL_001d: callvirt instance valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<!0> class [System.Private.CoreLib]System.Threading.Tasks.Task`1<string>::GetAwaiter()
IL_0022: stloc.1
// sequence point: hidden
IL_0023: ldloca.s 1
IL_0025: call instance bool valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string>::get_IsCompleted()
IL_002a: brtrue.s IL_0068
IL_002c: ldarg.0
IL_002d: ldc.i4.0
IL_002e: dup
IL_002f: stloc.0
IL_0030: stfld int32 C/'<B>d__1'::'<>1__state'
IL_0035: ldarg.0
IL_0036: ldloc.1
IL_0037: stfld valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string> C/'<B>d__1'::'<>u__1'
IL_003c: ldarg.0
IL_003d: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<B>d__1'::'<>t__builder'
IL_0042: ldloca.s 1
IL_0044: ldarg.0
IL_0045: call instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::AwaitUnsafeOnCompleted<valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string>, valuetype C/'<B>d__1'>(!!0&, !!1&)
IL_004a: leave.s IL_009c
IL_004c: ldarg.0
IL_004d: ldfld valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string> C/'<B>d__1'::'<>u__1'
IL_0052: stloc.1
IL_0053: ldarg.0
IL_0054: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string> C/'<B>d__1'::'<>u__1'
IL_0059: initobj valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string>
IL_005f: ldarg.0
IL_0060: ldc.i4.m1
IL_0061: dup
IL_0062: stloc.0
IL_0063: stfld int32 C/'<B>d__1'::'<>1__state'
IL_0068: ldloca.s 1
IL_006a: call instance !0 valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.TaskAwaiter`1<string>::GetResult()
IL_006f: pop
IL_0070: leave.s IL_0089
} // end .try
catch [System.Private.CoreLib]System.Exception
{
// sequence point: hidden
IL_0072: stloc.3
IL_0073: ldarg.0
IL_0074: ldc.i4.s -2
IL_0076: stfld int32 C/'<B>d__1'::'<>1__state'
IL_007b: ldarg.0
IL_007c: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<B>d__1'::'<>t__builder'
IL_0081: ldloc.3
IL_0082: call instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::SetException(class [System.Private.CoreLib]System.Exception)
IL_0087: leave.s IL_009c
} // end handler
IL_0089: ldarg.0
IL_008a: ldc.i4.s -2
IL_008c: stfld int32 C/'<B>d__1'::'<>1__state'
// sequence point: hidden
IL_0091: ldarg.0
IL_0092: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<B>d__1'::'<>t__builder'
IL_0097: call instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::SetResult()
IL_009c: ret
} // end of method '<B>d__1'::MoveNext
.method private final hidebysig newslot virtual
instance void SetStateMachine (
class [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine stateMachine
) cil managed
{
.custom instance void [System.Private.CoreLib]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = (
01 00 00 00
)
.override method instance void [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine::SetStateMachine(class [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine)
// Method begins at RVA 0x2250
// Code size 13 (0xd)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<B>d__1'::'<>t__builder'
IL_0006: ldarg.1
IL_0007: call instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::SetStateMachine(class [System.Private.CoreLib]System.Runtime.CompilerServices.IAsyncStateMachine)
IL_000c: ret
} // end of method '<B>d__1'::SetStateMachine
} // end of class <B>d__1
// Methods
.method public hidebysig static
class [System.Private.CoreLib]System.Threading.Tasks.Task A () cil managed
{
.custom instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncStateMachineAttribute::.ctor(class [System.Private.CoreLib]System.Type) = (
01 00 09 43 2b 3c 41 3e 64 5f 5f 30 00 00
)
// Method begins at RVA 0x2050
// Code size 47 (0x2f)
.maxstack 2
.locals init (
[0] valuetype C/'<A>d__0'
)
IL_0000: ldloca.s 0
IL_0002: call valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::Create()
IL_0007: stfld valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<A>d__0'::'<>t__builder'
IL_000c: ldloca.s 0
IL_000e: ldc.i4.m1
IL_000f: stfld int32 C/'<A>d__0'::'<>1__state'
IL_0014: ldloca.s 0
IL_0016: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<A>d__0'::'<>t__builder'
IL_001b: ldloca.s 0
IL_001d: call instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::Start<valuetype C/'<A>d__0'>(!!0&)
IL_0022: ldloca.s 0
IL_0024: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<A>d__0'::'<>t__builder'
IL_0029: call instance class [System.Private.CoreLib]System.Threading.Tasks.Task [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::get_Task()
IL_002e: ret
} // end of method C::A
.method public hidebysig static
class [System.Private.CoreLib]System.Threading.Tasks.Task B () cil managed
{
.custom instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncStateMachineAttribute::.ctor(class [System.Private.CoreLib]System.Type) = (
01 00 09 43 2b 3c 42 3e 64 5f 5f 31 00 00
)
// Method begins at RVA 0x208c
// Code size 47 (0x2f)
.maxstack 2
.locals init (
[0] valuetype C/'<B>d__1'
)
IL_0000: ldloca.s 0
IL_0002: call valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::Create()
IL_0007: stfld valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<B>d__1'::'<>t__builder'
IL_000c: ldloca.s 0
IL_000e: ldc.i4.m1
IL_000f: stfld int32 C/'<B>d__1'::'<>1__state'
IL_0014: ldloca.s 0
IL_0016: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<B>d__1'::'<>t__builder'
IL_001b: ldloca.s 0
IL_001d: call instance void [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::Start<valuetype C/'<B>d__1'>(!!0&)
IL_0022: ldloca.s 0
IL_0024: ldflda valuetype [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder C/'<B>d__1'::'<>t__builder'
IL_0029: call instance class [System.Private.CoreLib]System.Threading.Tasks.Task [System.Private.CoreLib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::get_Task()
IL_002e: ret
} // end of method C::B
} // end of class C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment