Skip to content

Instantly share code, notes, and snippets.

@zgramana
Created May 21, 2013 21:11
Show Gist options
  • Save zgramana/5623292 to your computer and use it in GitHub Desktop.
Save zgramana/5623292 to your computer and use it in GitHub Desktop.
Compares the difference between Action<T> and Func<T,bool> where we ignore the return value.
using System;
public static class Program
{
public static void Main(string[] args)
{
var m = new Action<int>(i => Console.WriteLine(i.ToString()));
m(5);
}
}
.class public auto ansi abstract sealed beforefieldinit Program
extends [mscorlib]System.Object
{
// Fields
.field private static class [mscorlib]System.Action`1<int32> 'CS$<>9__CachedAnonymousMethodDelegate1'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Methods
.method public hidebysig static
void Main (
string[] args
) cil managed
{
// Method begins at RVA 0x2060
// Code size 42 (0x2a)
.maxstack 2
.entrypoint
.locals init (
[0] class [mscorlib]System.Action`1<int32>
)
IL_0000: nop
IL_0001: ldsfld class [mscorlib]System.Action`1<int32> Program::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_0006: brtrue.s IL_001b
IL_0008: ldnull
IL_0009: ldftn void Program::'<Main>b__0'(int32)
IL_000f: newobj instance void class [mscorlib]System.Action`1<int32>::.ctor(object, native int)
IL_0014: stsfld class [mscorlib]System.Action`1<int32> Program::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_0019: br.s IL_001b
IL_001b: ldsfld class [mscorlib]System.Action`1<int32> Program::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_0020: stloc.0
IL_0021: ldloc.0
IL_0022: ldc.i4.5
IL_0023: callvirt instance void class [mscorlib]System.Action`1<int32>::Invoke(!0)
IL_0028: nop
IL_0029: ret
} // end of method Program::Main
.method private hidebysig static
void '<Main>b__0' (
int32 i
) cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2050
// Code size 14 (0xe)
.maxstack 8
IL_0000: ldarga.s i
IL_0002: call instance string [mscorlib]System.Int32::ToString()
IL_0007: call void [mscorlib]System.Console::WriteLine(string)
IL_000c: nop
IL_000d: ret
} // end of method Program::'<Main>b__0'
} // end of class Program
using System;
public static class Program
{
public static void Main(string[] args)
{
Func<int,bool> m = (i) => { Console.WriteLine(i.ToString()); return true; };
m(5);
}
}
.class public auto ansi abstract sealed beforefieldinit Program
extends [mscorlib]System.Object
{
// Fields
.field private static class [mscorlib]System.Func`2<int32, bool> 'CS$<>9__CachedAnonymousMethodDelegate1'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Methods
.method public hidebysig static
void Main (
string[] args
) cil managed
{
// Method begins at RVA 0x2070
// Code size 42 (0x2a)
.maxstack 2
.entrypoint
.locals init (
[0] class [mscorlib]System.Func`2<int32, bool>
)
IL_0000: nop
IL_0001: ldsfld class [mscorlib]System.Func`2<int32, bool> Program::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_0006: brtrue.s IL_001b
IL_0008: ldnull
IL_0009: ldftn bool Program::'<Main>b__0'(int32)
IL_000f: newobj instance void class [mscorlib]System.Func`2<int32, bool>::.ctor(object, native int)
IL_0014: stsfld class [mscorlib]System.Func`2<int32, bool> Program::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_0019: br.s IL_001b
IL_001b: ldsfld class [mscorlib]System.Func`2<int32, bool> Program::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_0020: stloc.0
IL_0021: ldloc.0
IL_0022: ldc.i4.5
IL_0023: callvirt instance !1 class [mscorlib]System.Func`2<int32, bool>::Invoke(!0)
IL_0028: pop
IL_0029: ret
} // end of method Program::Main
.method private hidebysig static
bool '<Main>b__0' (
int32 i
) cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2050
// Code size 20 (0x14)
.maxstack 1
.locals init (
[0] bool
)
IL_0000: nop
IL_0001: ldarga.s i
IL_0003: call instance string [mscorlib]System.Int32::ToString()
IL_0008: call void [mscorlib]System.Console::WriteLine(string)
IL_000d: nop
IL_000e: ldc.i4.1
IL_000f: stloc.0
IL_0010: br.s IL_0012
IL_0012: ldloc.0
IL_0013: ret
} // end of method Program::'<Main>b__0'
} // end of class Program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment