C# - 1 - Anything that can be disposed should be disposed.

[C# 2.0]

If something can be disposed it should. This goes for DataSet, DataTable, Bitmap, etc. Also, if you can hold a number of object references in something you should tidy that up too. For example, do you write code like this?

What happens is that you create an empty DataSet object with it's address stored in _MainData, then create a second DataSet and replace the address of the first DataSet with that of the second … thereby orphaning the first DataSet which is now leaked. Instead try this:

You don't have to add the set accessor to the interface if you don't want to expose it, but by always using the property accessor to create new DataSets you know any previeous DataSet objects are properly dealt with. Notice the presence of a Close method in the class: you must do some sort of final tidy up when the class is finished.

I'll leave you to figure out the purpose of setting the DataSet's Locale property to InvariantCulture (it also applies to DataTables).

You can use variations of this tip for ArrayLists, Hashtables, Bitmaps, and even your own classes.

.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License