idisposable

Help me understand Dispose() implementation code from MSDN

the following code is from MSDN: Idisposable pattern protected virtual void Dispose(bool disposing) { // If you need thread safety, use a lock around these // operations, as well as in your methods that use the resource. if (!_disposed) { if (disposing) { if (_resource != ...

Using Unity and interfaces, how do I create a concrete class that implements IDisposable

I have an interface (IDbAccess) for a database access class so that I can unit test it using Unity. It all works fine in Unity and now I want to make the concrete database class implement IDisposable so that it closes the db connections. My problem is that Unity does not understand that my concrete class is disposable because the interf...

When should I implement IDisposable?

What is the best practice for when to implement IDisposable? Is the best rule of thumb to implement it if you have one managed object in the class, or does it depend if the object was created in the class or just passed in? Should I also do it for classes with no managed objects at all? ...

How to make an IDisposable object a class variable?

I am working with Active Directory using C#. Instantiating the PrincipalContext object seems to be expensive, so I'd like to store one in a class variable. When using PrincipalContext as a local variable, I can use the convenient using syntax. When storing an IDisposable object in a static variable, how do I ensure the object is properl...

IDisposable, does it really matter

Coming from C/C++ a long time ago I still have a habit of ensuring that all resources are cleaned up correctly. I always ensure Dispose is called on IDisposable classes and implement Dispose patterns in my classes containing disposable objects. However, in my environment I'm more or less the only one doing this. Others just don't unders...

Implementing IDisposable on a subclass when the parent also implements IDisposable

I have a parent and child class that both need to implement IDisposable. Where should virtual (and base.Dispose()?) calls come into play? When I just override the Dispose(bool disposing) call, it feels really strange stating that I implement IDisposable without having an explicit Dispose() function (just utilizing the inherited one), b...

C# using statement with a null object

Is it safe to use the using statement on a (potentially) null object? I.e. consider the following example: class Test { IDisposable GetObject(string name) { // returns null if not found } void DoSomething() { using (IDisposable x = GetObject("invalid name")) { if (x != null) { //...

Should classes that are passed references to IDisposable objects be IDisposable as well and set them to null when disposed?

I think the question says it all. Thanks. ...

Long overdue (for me) question about disposing managed objects in .Net, VB.Net, C#

I can't believe I'm still confused about this but, any way, lets finally nail it: I have a class that overrides OnPaint to do some drawing. To speed things up, I create the pens, brushes etc before hand, in the constructor, so that OnPaint does not need to keep creating and disposing them. Now, I make sure that I always dispose of such...

Why isnt HashAlgorithm.Dispose public?

Why isnt HashAlgorithm.Dispose public? void IDisposable.Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } I understand that it is and explicit interface implementation and can still be called. I am trying to work out what the reasoning is behind it. ...

How to implement the disposable pattern in a class that inherits from another disposable class?

Hi, I often used the disposable pattern in simple classes that referenced small amount of resources, but I never had to implement this pattern on a class that inherits from another disposable class and I am starting to be a bit confused in how to free the whole resources. I start with a little sample code: public class Tracer : IDispo...

Should I use IDisposable for purely managed resources?

Here is the scenario: I have an object called a Transaction that needs to make sure that only one entity has permission to edit it at any given time. In order to facilitate a long-lived lock, I have the class generating a token object that can be used to make the edits. You would use it like this: var transaction = new Transaction();...

IDisposable: is it necessary to check for null on finally {}?

In most examples that you find on the web when explicitly not using "using", the pattern looks something like: SqlConnection c = new SqlConnection(@"..."); try { c.Open(); ... } finally { if (c != null) //<== check for null c.Dispose(); } If you do use "using" and look at the generated IL code, you can see that...

Checking for Objects that implement IDisposable but aren't properly disposed

Is there a way to automatically check existing C# source code for instances of objects that are not properly disposed of ie. using try / catch / finally or using statements? Or do I need to just manually look at the code? ...

If I replace an image in a PictureBox control, should I dispose the original image first? .Net Winforms.

Following on from my question here http://stackoverflow.com/questions/2548664/long-overdue-for-me-question-about-disposing-managed-objects-in-net-vb-net , If I replace an image in a picture box, should I dispose the original image first? Or, what about this situation: Dim bm As New Bitmap(32,32) bm = New Bitmap(32,32) bm = New Bi...

IOC containers and IDisposable

It was recommended to me that, when using an IOC container, I should change this: class Foobar: IFoobar, IDisposable {}; Into this: interface IFoobar: IDisposable{}; class Foobar : IFoobar{}; I'm wondering if this is ok, or if it solves one problem and creates another. It certainly solves the problem where I badly want to do this:...

How does one Dispose an object that is set in foreach?

foreach(var someDisposableObject in listOfDisposableObjects) { //some code someDisposableObject.Dispose(); //current code contains something like this. } Is there safe way, like a using clause to use in this scenario? For my second iteration (before getting responses) I changed the code to foreach(var someDisposableObject in...

Is it possible to force the use of "using" for disposable classes?

I need to force the use of "using" to dispose a new instance of a class. public class MyClass : IDisposable { ... } using(MyClass obj = new MyClass()) // Force to use "using" { } ...

Using using to dispose of nested objects

If I have code with nested objects like this do I need to use the nested using statements to make sure that both the SQLCommand and the SQLConnection objects are disposed of properly like shown below or am I ok if the code that instantiates the SQLCommand is within the outer using statement. using (var conn = new SqlConnection(sqlConnSt...

Proper disposing of class implementing IDisposable

Which one of the below 2 code pieces is not calling dispose and therefore is bad practice: ... using(SomeIDisposable p = new SomeIDisposable()) { return p.GetSomething(...); } ... or ... return new SomeIDisposable().GetSomething(...); ... ? ...