Given I've written a class that implements IEnumerable, and returns an IEnumerator that's not just IDisposable by nature of being an enumerator, but from having managed resources that it needs to Dispose() after it's done enumerating, can I trust that the following will Dispose() those managed resources as long as my enumerator properly disposes those managed resources in its IDisposable implementation?
return (new MyClass()).Select(x => x); EDIT: This question is seemingly similar to the one mods marked similar to it, but it's semantically different IMO (I saw it before I made the question)
Console.WriteLine()in yourDispose()and see if it gets fired.MyClassimplementsIDisposabletheDispose()method would get called. The garbage collector isn't the one that callsDispose().MyClass. In the OP's code.Disposewould not be called on thenew MyClass().Console.WriteLine()in theDispose()method.