Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a list of common object that implement IDisposable for the using statement?

I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection, MemoryStream, etc.

Taking it one step further, it would be great to even show the other "pieces of the puzzle", like how you should actually call connection.Close() before the closing using statement bracket.

Anything like that exist? If not, maybe we should make one.

like image 363
John B Avatar asked Jun 23 '09 15:06

John B


People also ask

What are IDisposable objects?

This article about the IDisposable pattern is the continuation of my previous article “Object LifeTime in . NET Framework". IDisposable is an interface that contains a single method, Dispose(), for releasing unmanaged resources, like files, streams, database connections and so on.

How do you know if a class implements IDisposable?

If a type implements the IDisposable interface, you should always call the Dispose method on an instance of the class when you are done using it. The presence of IDisposable indicates that the class has some resources that can be released prior to garbage collection.

Which of these are the reason for implementing IDisposable interface?

If your class creates unmanaged resources, then you can implement IDisposable so that these resources will be cleaned up properly when the object is disposed of. You override Dispose and release them there.

Why do we implement IDisposable interface in C#?

IDisposable is defined in the System namespace. It provides a mechanism for releasing unmanaged resources. When your application or class library encapsulates unmanaged resources such as files, fonts, streams, database connections, etc, they should implement the IDisposable interface or the IAsyncDisposable interface.


1 Answers

Perhaps glance at my post on this at http://www.lancemay.com/2010/01/idisposable-cheat-sheet/. Not sure if that's what you're looking for, but based on the original question, it sounds like it may be.

like image 140
Lance Avatar answered Oct 06 '22 00:10

Lance