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 ...
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))
{
...
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...
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 ...
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
}
...
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...
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 ...
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...
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...