1

I trying to see compiler's code for this little sample:

static void Main()
{
    int i = 0;
    AddToI(5);
    AddToI(10);
    Console.WriteLine(i);
    void AddToI(int amount) => i += amount;
}

I am using ILSpy and it is showing me code almost identical to the original. But I expected compiler to add some class or structure?

Maybe I can tune ILSpy to show me more details?

walruz
  • 1,135
  • 1
  • 13
  • 33
  • It was the goal of the ILSpy authors to make the output resemble C# as close as possible. If you want to see the soul of the machine then consider using ildasm.exe, run it from the Developer Command Prompt. – Hans Passant May 07 '22 at 13:21

1 Answers1

1

You can select a lower language version (e.g. C# 6) in the toolbar to prevent ILSpy from using newer features.

You also can turn off individual language features in View>Options>Decompiler.

To quickly see "what does this code compile to?", you can use https://sharplab.io. SharpLab's C# mode also uses the ILSpy decompiler, but with almost all features disabled.

Daniel
  • 15,944
  • 2
  • 54
  • 60
  • SharpLab is really cool - it did show me right away what I wanted (compiler generated class). I almost managed to get same result using ILSpy: I deselected "Introduce local functions". But it still does not show me compiler generated class itself. Maybe you know how to fix that? – walruz May 10 '22 at 18:31
  • 1
    You can enable View > Show all types and members. – Daniel May 11 '22 at 18:41