Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "DisplayClass" name mean when calling lambda?

Tags:

c#

.net

lambda

According to this answer when code uses local variables from inside lambda methods the compiler will generate extra classes that can have name such as c__DisplayClass1. For example the following (completely useless) code:

class Program
{
    static void Main()
    {
        try {
            implMain();
        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }
    }

    static void implMain()
    {
        for (int i = 0; i < 10; i++) {
            invoke(() => {
                Console.WriteLine(i);
                throw new InvalidOperationException();
            });
        }
    }
    static void invoke(Action what)
    {
        what();
    }
}

outputs the following call stack:

System.InvalidOperationException
at ConsoleApplication1.Program.<>c__DisplayClass2.<implMain>b__0()
at ConsoleApplication1.Program.invoke(Action what)
at ConsoleApplication1.Program.implMain()
at ConsoleApplication1.Program.Main()

Note that there's c__DisplayClass2 in there which is a name of a class generated by the compiler to hold the loop variable.

According to this answer c__DisplayClass "means"

c --> anonymous method closure class ("DisplayClass")

Okay, but what does "DisplayClass" mean here?

What does this generated class "display"? In other words why is it not "MagicClass" or "GeneratedClass" or any other name?

like image 394
sharptooth Avatar asked May 06 '13 15:05

sharptooth


3 Answers

From an answer to a related question by Eric Lippert:

The reason that a closure class is called "DisplayClass" is a bit unfortunate: this is jargon used by the debugger team to describe a class that has special behaviours when displayed in the debugger. Obviously we do not want to display "x" as a field of an impossibly-named class when you are debugging your code; rather, you want it to look like any other local variable. There is special gear in the debugger to handle doing so for this kind of display class. It probably should have been called "ClosureClass" instead, to make it easier to read disassembly.

like image 101
svick Avatar answered Sep 17 '22 12:09

svick


You can get some insight from the C# compiler source as available from the SSCLI20 distribution, csharp/sccomp subdirectory. Searching the code for "display" gives most hits in the fncbind.cpp source code file. You'll see it used in code symbols as well as comments.

The comments strongly suggest that this was a term used internally by the team, possibly as far back as the design meetings. This is .NET 2.0 vintage code, there was not a lot of code rewriting going on yet. Just iterators and anonymous methods, both implemented in very similar ways. The term "display class" is offset from "user class" in the comments, a clear hint that they used the term to denote auto-generated classes. No strong hint why "display" was favored, I suspect that it might have something to do with these classes being visible in the metadata of the assembly.

like image 35
Hans Passant Avatar answered Sep 21 '22 12:09

Hans Passant


Based on Reflector, DisplayClass can be translated as CompilerGeneratedClass

[CompilerGenerated]
private sealed class <>c__DisplayClass16b
{
// Fields
public MainForm <>4__this;
public object sender;

// Methods
public void <cmdADSInit_Click>b__16a()
  {
    ADS.Initialize();
    this.<>4__this._Sender = this.sender;
    this.<>4__this.SelectedObject = ADS.Instance;
  }
}
like image 38
justromagod Avatar answered Sep 19 '22 12:09

justromagod