ashmind / SharpLab

.NET language playground
https://sharplab.io
BSD 2-Clause "Simplified" License
2.73k stars 201 forks source link

Static LINQ expression throws "Type C+<>c has a static constructor" #1347

Open znakeeye opened 3 weeks ago

znakeeye commented 3 weeks ago

Maybe same as those other static related issues, but here I don't see a constructor... Should be fairly easy to support this?

using System.Linq;
using System.Collections.Generic;

public class C {
    public void M() {
        List<string> list = [];        
        var a = list.Select(static s=> s.Length < 10).ToList();
    }
}
KeterSCP commented 8 hours ago

If you look at the Roslyn-generated code, you can see that there is a static object for the closure that is being initialized. So static constructor is automatically generated for it. IL output confirms this:

// Methods
.method private hidebysig specialname rtspecialname static
    void .cctor () cil managed