Hi all,
I'm for a C++ background and am moving into C#. I've read a lot of posts and the MSDN entries on the use of "IDisposable" but I want to make sure I've got a right end of the stick before setting up any bad habits.
I've created a Class e.g "Class1" that has among other functions and variables, contains an "SQLConnection" object. Initially I had a method along the lines of Class1.Close to call the SqlConnection.Dispose() when I had finished with the class.
It seems to make more sense to me to implement the Dispose(); method for my Class so I can take advantage of "Using" and be more in line with the language standard.
So far I've followed the MSDN template producing something like this:
public class Class1: IDisposable
{
// Other variables etc
private SqlConnection MISDBConnection = null;
private bool IsDisposed = false;
~Class1()
{
Dispose(false);
}
protected void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
// Code to dispose of managed resources
}
}
// Code to dispose of unmanaged resources
try
{
this.MISDBConnection.Dispose();
IsDisposed = true;
}
catch (SqlException e) // Temporary
{
string ErrMessage = "";
string ErrCaption = "";
ErrMessage = "Error whilst disposing of Sql Connection:\n\n" + e.Message;
ErrCaption = "Error Number - " + Convert.ToString(e.Number);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
Will that do the job and function correctly? Sometimes I notice people dispose of the managed and unmanaged in the same part of the if statement?
Am I correct in placing a try-catch around the Dispose method of the SQLConnection?
Finally, I'm slightly unclear on the Finalize in this instance, will I have to explicitly specifiy that my ints, strings, other managed objects are deleted by my own fair hand, or will the GC still take care of them?
Thanks for reading all that, and any responses!
Loading
Sam HobbsPosted Mar 10, 2010, 1:39 PM
ChrisPosted Mar 10, 2010, 4:30 AM
Thanks for the reply. I did read the documentation, I was concerned that I'd somehow surpressed the garbage collection on the managed members of my class (my "// Code to dispose of managed resources" contains nothing at the moment"). I'm enjoying many of the features C# has to offer so far, but some of the features do make me a little twitchy =D. I did a little messing with the debugger to watch some of the non-deterministic garbage collection work it's magic which settled my mind a bit.
I'm reading this page at the moment "codeproject.com/KB/dotnet/idisposable.aspx" which looks like it will give me a chunk more information regarding the topic.
Thanks again =D
Sam HobbsPosted Mar 9, 2010, 2:59 PM
Did you read the IDisposable Interface documentation? Especially the part that says that the "primary use of this interface is to release unmanaged resources"?
I am still learning C# also, but as far as I know, we don't need to worry about disposing our managed objects. We are supposed to trust garbage collection. Those of us familiar with C++ will likely consider garbage colletion to support sloppy programming, but that is how the language is designed to work. I assume C# uses garbage collection because Java does.
Something else that programmers experienced with C++ are likely to dislike is Implicitly Typed Local Variables; the kind declared using var. Note that a var is local to the function it is in and cannot be returned from a function. Microsoft likes to use them but I sure don't. Something else that Microsoft likes to do is casting; C# and .Net is designed in a way that requires casts much more than a good C++ programmer would.
There are many other gotchas. One surprising one is that a C# array is fixed in size, but there might be a way arround that. To get an array that is dynamic in the manner that a std::vector is, you need to use an ArrayList.