affiliate_link

Monday, December 18, 2017

How do I know if a class in C# is unmanaged, whether I have to implement the IDisposable interface?

StreamReader is not an unmanaged resource itself, but it wraps one, and therefore it implements IDisposable. Anything that implements IDisposable should be disposed. Other than that, you don't have to worry about unmanaged resources unless you're using Windows API calls via PInvoke (because anything that wraps resources allocated that way should implement IDisposable)

using (StreamReader sr = new StreamReader(filename)) {
     txt = sr.ReadToEnd();
}

No comments: