Created
July 31, 2017 01:52
-
-
Save waf/2e313bcfad611701b8692c9fcaa5a719 to your computer and use it in GitHub Desktop.
How are C# Local Functions Implemented
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static int AddFive(int a) | |
| { | |
| // object of a compiler-generated type is created | |
| // reference to a compiler-generated method | |
| return Program.<AddFive>g__InnerAdd1_0(5, ref new Program.<>c__DisplayClass1_0() | |
| { | |
| a = a | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static int AddFive(int a) | |
| { | |
| // object of a compiler-generated type is created | |
| Program.<>c__DisplayClass1_0 cDisplayClass10 = new Program.<>c__DisplayClass1_0(); | |
| // reference to a compiler-generated field | |
| cDisplayClass10.a = a; | |
| // reference to a compiler-generated method | |
| Program.<AddFive>g__InnerAdd1_0(5, ref cDisplayClass10); | |
| // reference to a compiler-generated field | |
| return cDisplayClass10.a; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static int AddFive(int a) | |
| { | |
| void InnerAdd(int b) => a += b; | |
| InnerAdd(5); | |
| return a; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .method assembly hidebysig static int32 | |
| '<AddFive>g__InnerAdd1_0'( | |
| int32 b, | |
| [in] valuetype Demo.Program/'<>c__DisplayClass1_0'& obj1 | |
| ) cil managed | |
| { | |
| .custom instance void | |
| [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() | |
| = (01 00 00 00 ) | |
| .maxstack 8 | |
| IL_0000: ldarg.1 // obj1 | |
| IL_0001: ldfld int32 Demo.Program/'<>c__DisplayClass1_0'::a | |
| IL_0006: ldarg.0 // b | |
| IL_0007: add | |
| IL_0008: ret | |
| } // end of method Program::'<AddFive>g__InnerAdd1_0' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .class nested private sealed auto ansi beforefieldinit | |
| '<>c__DisplayClass1_0' | |
| extends [mscorlib]System.ValueType | |
| { | |
| .custom instance void | |
| [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() | |
| = (01 00 00 00 ) | |
| .field public int32 a | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Console.WriteLine(AddFive(7)); | |
| } | |
| static int AddFive(int a) | |
| { | |
| // the local function declaration | |
| int InnerAdd(int b) => a + b; | |
| return InnerAdd(5); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment