6

I'm reading Async in C# 5.0, and the section on the compiler transform contains this snippet:

public Task<int> AlexsMethod()
{
    <AlexsMethod>d__0 stateMachine = new <AlexsMethod>d__0();
    stateMachine.<>4__this = this;
    stateMachine.<>t__builder = AsyncTaskMethodBuilder<int>.Create();
    stateMachine.<>1__state = -1;
    stateMachine.<>t__builder.Start<<AlexsMethod>d__0>(ref stateMachine);
    return stateMachine.<>t__builder.Task;
}

There are two pieces of notation that are new to me. The first is <AlexsMethod>d__0. The second is stateMachine.<>4__this. Neither works when I try them myself, so I suspect it's for use by the compiler only. But I'm having trouble searching for more information on what is intended by this notation.

Matthew
  • 28,056
  • 26
  • 104
  • 170
  • 2
    Someone made a post on SO once showing a lot of what the C# compiler (at the time - subject to change) made - all of the random little characters (e.g. the `4` in `4__this`) you see have an implicit meaning. Edit: here it is: http://stackoverflow.com/questions/2508828/where-to-learn-about-vs-debugger-magic-names – Tim S. Jun 15 '13 at 18:21

2 Answers2

8

Unlike the brackets marking generics (e.g. Task<int>), they don't have special meaning. They're just what the compiler generates - identifiers that are valid in IL, but not in C#.

Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • And the code which generates the names is [here](https://github.com/dotnet/roslyn/blob/master/src/Compilers/CSharp/Portable/Symbols/Synthesized/GeneratedNames.cs) (borrowed from [this answer](https://stackoverflow.com/a/2509524/774575)) – mins Jul 06 '19 at 06:18
0

When you use lambda expression, lambda expression is a anonymous method. But compiler need to give it a name, so these methods are generated by compiler, it is not readable for human.

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
ntwo1980
  • 11
  • 1