What you're seeing is not a memory leak. It's simply the way the compiler is caching your Comparison<IndicatorPropReport>
as a static
delegate at the call site, thus saving you the need to create an instance of it for each invocation.
If you look at this simplified example:
var ints = new List<int> { 3, 2, 1, 8, 5 };
ints.Sort((x, y) => x.CompareTo(y));
And look at what the compiler generates using an .NET decompiler:
[CompilerGenerated]
private static Comparison<int> CS$<>9__CachedAnonymousMethodDelegate2;
public static void Main(string[] args)
{
List<int> ints = new List<int> { 3,2,1,8,5 };
List<int> arg_51_0 = ints;
if (Program.CS$<>9__CachedAnonymousMethodDelegate2 == null)
{
Program.CS$<>9__CachedAnonymousMethodDelegate2 =
new Comparison<int>(Program.<Main>b__1);
}
arg_51_0.Sort(Program.CS$<>9__CachedAnonymousMethodDelegate2);
}
[CompilerGenerated]
private static int <Main>b__1(int x, int y)
{
return x.CompareTo(y);
}
You see that the Comparsion<int>
was cached as a static
delegate. The same behavior is what happens in your method call.
Note this behavior is pre Roslyn. Roslyn changes the way delegates are cached by creating a display class instead of the static delegate, even when there's no captured variables.