idisposable

Finalizer and IDisposable

Based on the documentation (MSDN: link), it is clear that one should use the IDisposable pattern when implementing a finalizer. But do you need to implement a finalizer if you implement IDisposable (so as to provide a deterministic way of disposing the object), and you dont have any unmanaged resources to clean up? As I see it, if the ...

Why disposing StreamReader makes a stream unreadable?

Hi, I need to read a stream two times, from start to end. But the following code throws an ObjectDisposedException: Cannot access a closed file exception. string fileToReadPath = @"<path here>"; using (FileStream fs = new FileStream(fileToReadPath, FileMode.Open)) { using (StreamReader reader = new StreamReader(fs)) { ...

Any way to clear all WithEvents fields via one command?

The vb.net "WithEvents" syntax is very useful, but if a WithEvents field which references a long-lived object is not nulled out, it will often constitute a memory leak. Is there any easy way for a Dispose routine to have vb.net automatically clear out all WithEvents fields and unsubscribe them? I've figured out a nice way to wrap the c...

C# disposing objects

I know the Dispose() method is called on the StreamReader object when you have the following code: //Sample 1 using (StreamReader sr1 = new StreamReader(@"C:\Data.txt")) { string s1 = sr1.ReadToEnd(); //Do something with s1... } But if you write the code like this (Sample 2) will the Dispose() method get called too? //Sample ...

.NET - Replacing nested using statements with single using statement

If you came across some C# code like this with nested using statements/resources: using (var response = (HttpWebResponse)request.GetResponse()) { using (var responseStream = response.GetResponseStream()) { using (var reader = new BinaryReader(responseStream)) { // do something with reader } ...

How are IDisposable objects passed to base constructor arguments handled in C# if initialization fails?

If I pass an IDisposable object to a base class constructor using the base(...) construct, I trigger a silly error in FxCop about letting loose of an IDisposable. The warning is occasionally useful elsewhere, so I don't want to disable it, but I realize I don't know the exact semantics I should be using. Is it the base constructor's res...

Question on IDisposable

Hi, My question is why do I need IDisposable? I have a class that consumes some resources that need to be freed. I have two options Without IDisposable class SomeUtilityClass { public Stop() { // free resources } } With IDisposable class SomeUtilityClass, IDisposable { public void Dispose() { // free ...

using statement on IDisposable object - delay of calling Dispose method

As describe this article, about usage of using on IDisposable objects, it says one interesting words: ...using block, the Dispose method is automatically called sometime after the block ends. (It may not be immediate; it depends on the CLR.) Interesting here is "It may not be immediate; it depends on the CLR". Can anyone provide more...

Is this a good way to manage Disposable objects in ASP.NET MVC?

I am overriding the Controller.Dispose(bool) method in my ASP.NET MVC2 Controllers in order to dispose of things as needed, while leaving them alive for as long as possible. This is as opposed to disposing of them prior to returning from the Controller's action method. My question, in short; does this work as I expect? From what I'm se...