Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the compiler automatically generate a Debugger attribute for anonymous type?

When compiling a simple anonymous type code like this:

public class Test
{   
    public static void Main()
    {
        var my_anonymous = new { Name = "anonymous"};
    }
}

The IL code has a Debugger attribute for every generated method of the anonymous type. e.g. for Equals:

.method public hidebysig virtual instance bool 
          Equals(object 'value') cil managed
  {
    .custom instance void [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = ( 01 00 00 00 )
  ... // rest of IL code

Why does the compiler generate it automatically? I compiled it with Microsoft (R) Visual C# Compiler version 4.6.1055.0, and (if this has any relevance) without using the VS cmd.

Note: There's this answer for removing the attribute (not possible), but I wonder about the "Why?".

like image 728
OfirD Avatar asked Jan 05 '23 10:01

OfirD


1 Answers

Because anonymous type implementation is automatically done by the compiler and there is no point in having the debugger step through the auto generated code.

  1. Because there is no bug in the generated code; it would be a compiler bug, not something specific to your code.
  2. Becuse even if there was a bug you could do little about it as the class would be rewritten once you compile.
  3. Because stepping through uninteresting code every time you debug is annoying.
like image 125
InBetween Avatar answered Jan 14 '23 13:01

InBetween