Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Created February 16, 2023 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasdarimont/e2a7c2dafce044c8d0f683f3ed91fd7d to your computer and use it in GitHub Desktop.
Save thomasdarimont/e2a7c2dafce044c8d0f683f3ed91fd7d to your computer and use it in GitHub Desktop.
C# top-level statements to lowered C#

Original C# using top-level statements

// See https://aka.ms/new-console-template for more information
var a = 2;
int s = 0; 
var inc = (int x) => s = x + 1;
var c = inc(a);
Console.WriteLine("c={0} s={1}", c ,s); // c=3 s=3

Lowered C#

// Decompiled with JetBrains decompiler
// Compiler-generated code is shown

using System;
using System.Runtime.CompilerServices;

[CompilerGenerated]
internal class Program
{
  private static void <Main>$(string[] args)
  {
    Program.<>c__DisplayClass0_0 cDisplayClass00 = new Program.<>c__DisplayClass0_0();
    int num = 2;
    cDisplayClass00.s = 0;
    Console.WriteLine("c={0} s={1}", (object) new Func<int, int>((object) cDisplayClass00, __methodptr(<<Main>$>b__0))(num), (object) cDisplayClass00.s);
  }

  public Program()
  {
    base..ctor();
  }

  [CompilerGenerated]
  private sealed class <>c__DisplayClass0_0
  {
    public int s;

    public <>c__DisplayClass0_0()
    {
      base..ctor();
    }

    internal int <<Main>$>b__0(int x)
    {
      return this.s = x + 1;
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment