user3163495
user3163495

Reputation: 3672

Does the garbage collector know to dispose variables located in a lambda-captured scope?

In this C# example, will the garbage collector dispose of hugeString after TestClass is instantiated?

Furthermore, would uncommenting the line hugeString = null help garbage collection at all?

public sealed class TestClass
{
    public TestClass()
    {
        var hugeString = new string('0', 100000000);
        var length = hugeString.Length;
        GetLength = () => length;
        // hugeString = null; // do I need to do this?
    }
    public readonly Func<int> GetLength;
}

Upvotes: 0

Views: 82

Answers (0)

Related Questions