About Links Archives Search Feed Albums of Note


IDisposable - Why and When

I like using the C# using keyword. It’s great in dealing with connections and transactions, disposing of resources as we go, keeping an app lean and mean, so to speak. So maybe it’s not surprising that I figured out maybe I should do the same in my little app. So here’s the suggestion.static void Main(string[] args) { XmlSchema schema; CodeNamespace codeNamespace;

//Load schema into memory     using (SchemaLoader loader = new SchemaLoader())     {         schema = loader.LoadSchemaFromFile(args[0]);     }

//Convert schema into namespace     using (SchemaToNamespaceConverter converter = new SchemaToNamespaceConverter())     {         codeNamespace = converter.ConvertSchemaToCodeNamespace(schema);     }

//Manipulate namespace     using (CodeNamespaceTweaker tweaker = new CodeNamespaceTweaker())     {         tweaker.ChangeAllFieldPropertyNameCombosInNamespace(codeNamespace);     }

//Write the new code to a file     using (NamespaceToFileWriter writer = new NamespaceToFileWriter())     {         writer.WriteNamespaceToFile(codeNamespace, args[1]);     } }

And from this, several questions issue: 1. This is overkill right? 2. So when should a class really inherit from IDisposable so you can use it in a using statement? Microsoft’s 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 you’ll want disposed before the garbage collector would do it normally anyway. ergo not for convenience. But really, when do you actually implement it? Answers in comments please? 3. 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?

static void Main(string[] args) { XmlSchema schema; CodeNamespace codeNamespace;

//Load schema into memory     SchemaLoader loader = new SchemaLoader();     schema = loader.LoadSchemaFromFile(args[0]);

//Convert schema into namespace     SchemaToNamespaceConverter converter = new SchemaToNamespaceConverter();     codeNamespace = converter.ConvertSchemaToCodeNamespace(schema);

//Manipulate namespace     CodeNamespaceTweaker tweaker = new CodeNamespaceTweaker();     tweaker.ChangeAllFieldPropertyNameCombosInNamespace(codeNamespace);

//Write the new code to a file     NamespaceToFileWriter writer = new NamespaceToFileWriter();     writer.WriteNamespaceToFile(codeNamespace, args[1]); }

Answers anyone?

[Updated 13/11/2006]

Philip Rieck and a few others answered this question for me on the C# forum [http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=917355&SiteID=1] along with a few links for more information on IDisposable. Great thanks to them. For quick reference, the answers are. 1. Yes it is. 2. Implement IDisposable when you want a resource freed quickly rather than an object. Calling Dispose won’t free an object any faster than not implementing IDisposable and waiting for the garbage collector to show up. 3. No. The Garbage collector will take as long as it usually takes to collect an object whether you call finalize or not.

Do look at the more comprehensive answers in the post on the C# forums [http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=917355&SiteID=1]. You learn something every day.

Posted on November 11, 2006   #Geek Stuff  






← Next post    ·    Previous post →