IDisposable : Why and When To Use It?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
Im writing a little command line app that has four distinct sections of processing that Ive encapsulated in four different classes. To make it explicit for future development, I wanted to do something like this. <pre class=csharpcode><span class=kwrd>static <span class=kwrd>void Main(<span class=kwrd>string[] args)
{
XmlSchema schema;
CodeNamespace codeNamespace;

<span class=rem>//Load schema into memory
<span class=kwrd>using (SchemaLoader loader = <span class=kwrd>new SchemaLoader())
{
schema = loader.LoadSchemaFromFile(args[0]);
}

<span class=rem>//Convert schema into namespace
<span class=kwrd>using (SchemaToNamespaceConverter converter = <span class=kwrd>new SchemaToNamespaceConverter())
{
codeNamespace = converter.ConvertSchemaToCodeNamespace(schema);
}

<span class=rem>//Manipulate namespace
<span class=kwrd>using (CodeNamespaceTweaker tweaker = <span class=kwrd>new CodeNamespaceTweaker())
{
tweaker.ChangeAllFieldPropertyNameCombosInNamespace(codeNamespace);
}

<span class=rem>//Write the new code to a file
<span class=kwrd>using (NamespaceToFileWriter writer = <span class=kwrd>new NamespaceToFileWriter())
{
writer.WriteNamespaceToFile(codeNamespace, args[1]);
}
}
[/code]
<style>
.csharpcode, .csharpcode pre
{font-size:x-small;color:black;font-family:consolas, "Courier New", courier, monospace;background-color:#ffffff;}
.csharpcode pre
{margin:0em;}
.csharpcode .rem
{color:#008000;}
.csharpcode .kwrd
{color:#0000ff;}
.csharpcode .str
{color:#006080;}
.csharpcode .op
{color:#0000c0;}
.csharpcode .preproc
{color:#cc6633;}
.csharpcode .asp
{background-color:#ffff00;}
.csharpcode .html
{color:#800000;}
.csharpcode .attr
{color:#ff0000;}
.csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.csharpcode .lnum
{color:#606060;}
</style>

So that means implementing IDisposable in each of the four classes, which as they dont do anything with handles or unsafe code all have the same (non-)implmentation as derived from MSDN docs.
<span style="color:blue public <span style="color:blue void Dispose()
{
   GC.SuppressFinalize(<span style="color:blue this);
}

And from this, several questions issue:
<ol>
IS this implementation of IDisposable right?
Is this use of IDisposable so I can use the using statement overkill?
So when should a class really inherit from IDisposable so you can use it in a using statement? Microsofts guidelines for implementing it infer that you only need to do so if the class uses a Handle to point in memory, or uses an object which youll want disposed before the garbage collector would do it normally anyway. ergo not for convenience. But really, when do you actually implement it?
If I leave my code as it is above, do each of the classes in the using statements actually get finalized garbage collected faster than if I wrote something like the following?</ol><pre class=csharpcode><span class=kwrd>static <span class=kwrd>void Main(<span class=kwrd>string[] args)
{
XmlSchema schema;
CodeNamespace codeNamespace;

<span class=rem>//Load schema into memory
SchemaLoader loader = <span class=kwrd>new SchemaLoader();
schema = loader.LoadSchemaFromFile(args[0]);

<span class=rem>//Convert schema into namespace
SchemaToNamespaceConverter converter = <span class=kwrd>new SchemaToNamespaceConverter();
codeNamespace = converter.ConvertSchemaToCodeNamespace(schema);

<span class=rem>//Manipulate namespace
CodeNamespaceTweaker tweaker = <span class=kwrd>new CodeNamespaceTweaker();
tweaker.ChangeAllFieldPropertyNameCombosInNamespace(codeNamespace);

<span class=rem>//Write the new code to a file
NamespaceToFileWriter writer = <span class=kwrd>new NamespaceToFileWriter();
writer.WriteNamespaceToFile(codeNamespace, args[1]);
}
[/code]

View the full article
 
Back
Top