IDisposible makes it easy to do what you're doing. The whole point of
using it is so you don't have to explicitly call Dispose() - instead,
you wrap your code around a "using" block. The using block will call
Dispose() automatically at the end, or if an exception is thrown.
You can do whatever you want inside of Dispose() - close connections,
save files, etc. The method itself is just a normal method, that can
do anything any other method can do. It doesn't make the object
itself null or automatically take it out of scope, but in most cases
it does make the object unusable. As long as you create the instance
properly, you won't have any problems.
Example:
using (MyClass myInstance = new MyClass()) {
// Do stuff with myInstance
}
// myInstance is now out of scope
is the same as:
{
MyClass myInstance = new MyClass();
try {
// Do stuff with myInstance
}
finally {
myInstance.Dispose();
}
}
If handled properly, like the above "using" block, then your variable
is only in scope inside the block, so it's out of scope after Dispose
() is called, and you don't have to worry about things happening after
Dispose() is called. Once it's out of scope, then it's eligible for
garbage collection, so eventually the destructor will be called - but
since all of your important stuff was taken care of in Dispose(), you
shouldn't have to worry about the destructor.
You'll probably find hundreds of examples out there - framework
classes that implement IDisposible are things like SqlConnection (it
closes the connection upon Dispose()) and StreamWriter (it saves and
closes the file upon Dispose()). In real life, I've built an
IDisposible that does something like the following:
class Request : IDisposible {
public Request() {
Success = false; // already false by default, but for
illustration purposes
}
/* Several properties */
public bool Success { get; set; }
private void Save() { /* write to DB */ }
void IDisposible.Dispose() { Save(); }
}
In this case, I can call it like:
using (Request request = new Request()) {
// Do stuff
request.Success = true;
}
// If things went smoothly, then my object got written to the database
with Success = true
// If an exception was thrown, then my object got written to the
database with Success = false, with no extra coding here.
On Mar 19, 5:32 am, Alon K <[email protected]> wrote:
> Hi All,
>
> First let me state how frustrated I am with how "safe" they made C# to
> the point where it feels unwieldy.
>
> I can't wrap my head around how to properly use this interface, when
> it should be used and how to implement it correctly without leaving
> stuff trailing, and can't find any good explanations by searching the
> internet either.
>
> I am mostly interested in understanding the following aspects:
>
> 1. I have an instance"MyClass MyInstance = new MyClass();", I want to
> call "MyInstance.Dispose()" what do I need to do to the class? What
> happens to "MyInstance", is it just null now? Explain the
> implementation of the method please.
>
> 2. If I explicitly implement a Dispose() method, am I now forced to
> call it when an instance needs to be disposed? For example say I leave
> the scope of the instance, will the dispose method be called? If so,
> will it be the base or overriden one?
>
> 3. If I must implement a Dispose() method, is there a way to call a
> 'standard' or 'base' dispose method from the overriden method that
> will clear everything else normally or will I need to define the
> default actions in my version of Dispose(). In other words, say I need
> to create a destructor just for the sake of handling one or two
> special situations within my class, but aside from that everything
> else should be disposed of normally. For example, say I have an odbc
> connection/command defined in my class and I want to ensure that any
> running command is canceled and the connection closed before I destroy
> the class so all I really need that is out of the ordinary is a
> "command.Cancel();" and "connection.Close()", everything else should
> be the same, is there a way to do this simply.
>
> If I can get some answers to these, preferably with some solid
> examples I think I would feel more comfortable.
>
> Thanks in advance!