idisposable

Looking for Stylecop-perfect comments for `protected override void Dispose(bool disposing)`.

I know, StyleCop is not perfect, but we try to use it in a helpful way. I do like the fact that it complains about undocumented arguments. Now, for properties and constructors it recommends what the text should be, but it does not help with Dispose method, and I think it should. We have many classes that implement IDisposable. In this pa...

How to write a unit-test for the outcome of Dispose()

Is there a good way of testing the result of the IDisposable.Dispose()-method? Thanks for all your answers. ...

Ensuring Dispose is called when .net class expsed to COM

I've exposed a .net class to COM. The class inherits from IDisposable because I need to clean up some unmanaged resources. In the .net environment I would wrap my class in a using scope to ensure that Dispose() always gets called. Is there some facility to do this if I'm using the COM wrapper? I'm insantiating the class from VB6 and ...

IDisposable vs using keyword

If I am using the using keyword, do I still have to implement IDisposable? ...

LINQ query and IDisposable

I'm currently writing a piece of code that does some searches which returns IDisposable objects (DirectoryEntry to be specific from an ADAM instance) and I end up with code similar to using(var entry = (from result in results let entry = result.GetDirectoryEntry() where entry != null ...

Why should Dispose() be non-virtual?

I'm new to C#, so apologies if this is an obvious question. In the MSDN Dispose example, the Dispose method they define is non-virtual. Why is that? It seems odd to me - I'd expect that a child class of an IDisposable that had its own non-managed resources would just override Dispose and call base.Dispose() at the bottom of their own ...

Will SqlConnection get disposed by GC?

Disclaimer: I know IDisposable should be implemented when dealing with unmanaged resources. The rest of the code should be deterministic and do using (...) { } (equivalent of try {} finally { Dispose(); }) to guarantee a cleanup as soon as possible. Also, the GC will not call Dispose(), so the recommended pattern is to override the Fin...

Ensuring IDisposable call on objects created in the controller and handed off to view

I have always known that all good programmers call Dispose on any object that implements IDisposable, case in point the ObjectContext class in EF. I am new to asp.net mvc so this may be a noob question but here goes... public ActionResult Index() { using (var db = new MyObjectContext()) { return Vie...

IQueryable Repository with StructureMap (IoC) - How do i Implement IDisposable?

If i have the following Repository: public IQueryable<User> Users() { var db = new SqlDataContext(); return db.Users; } I understand that the connection is opened only when the query is fired: public class ServiceLayer { public IRepository repo; public ServiceLayer(IRepository injectedRepo) { this.repo = injec...

How do you deal with sequences of IDisposable using LINQ?

What's the best approach to call Dispose() on the elements of a sequence? Suppose there's something like: IEnumerable<string> locations = ... var streams = locations.Select ( a => new FileStream ( a , FileMode.Open ) ); var notEmptyStreams = streams.Where ( a => a.Length > 0 ); //from this point on only `notEmptyStreams` will be used/v...

Use Dispose() or finalizer to clean up managed threads?

Suppose I have a message pump class in C++0x like the following (note, SynchronizedQueue is a queue of function<void()> and when you call receive() on the queue and it is empty, it blocks the calling thread until there is an item to return): class MessagePump { private: bool done_; Thread* thread_; SynchronizedQueue queue_;...

Disposing IDisposable items generated by Observable

I have an Observable<WebResponse> (WebResponse implements IDisposable) responseObservable .Where(webResponse => webResponse.ContentType.StartsWith("text/html")) .Select(webResponse => webResponse.ContentLength) .Run() (Ignore the pointlessness of the query!) so, I'm discarding WebResponse instances without calling Dispose...

Specific questions about C# Dispose Pattern

I have a few basic questions about the Dispose Pattern in C#. In the following code snippet, which seems to be a standard way of implementing the dispose pattern, you’ll notice that managed resources are not handled if disposing is false. How/when are they handled? Does the GC come along and handle the managed resources later? But if...

Handling iDisposable in failed initializer or constructor

Is there any nice pattern in .Net for ensuring that iDisposable fields owned by an object will get disposed if an exception is thrown during construction, possibly during a field initializer? The only way to surround field initializers in a Try/Catch block is if the block is outside the call to the constructor, which will make it rather...

Disposing a non-component when a form disposes?

I have a form that has a member that implements IDisposable but not IComponent. I need to dispose it when the form disposes. Unfortunately the form's dispose is already implemented in the automatically generated portion of the code, and is not partial. How can I dispose this object? ...

Saving a Class to disk on dispose: Does my code have bugs?

I am attempting to make a simple class that serializes itself to disk when it is no longer in use. The code I have right now (see below). The code I have now seems to work, but I am not fully confident in my knowledge, so I am wondering if anyone else sees any significant problems with this code. void IDisposable.Dispose() { Dispo...

Writing our own Dispose method instead of using Idisposable

After going through a lot of articles of Idisposable i got confused about it usage. All articles explain what is it and how to implement. I want to understand what we will miss if we don't have it. It is an interface with one method Dispose() in it. Let's take an example Often the use of dispose is shown as disposing a database connectio...

What does "opening a connection" actually mean?

I was trying to explain to someone why database connections implement IDisposable, when I realized I don't really know what "opening a connection" actually mean. So my question is - What does c# practically do when it opens a connection? Thank you. ...

Is 'using' a C# keyword?

If an object realizes IDisposable in C# one can write using(DisposableFoo foo = new DisposableFoo()) I've always wondered why the using looks like a C# keyword, but requires an interface defined in the .Net framework. Is using a keyword (in this context, obviously it is for defining your library imports), or has Microsoft overloaded i...

Do native method reference have to be disposed - and if so are there any recommended practices to do so

I have a class which uses a method in user32.dll: [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr windowHandlerPtr); According to Effective C#, should all classes which uses unmanaged code implement both IDisposable and a finalizer. Without going into the details in that d...