Reputation: 6490
A lot of people on SO pointed out that I should use using
statement more often. So I am practicing.
The problem is that I can't decide when to use it and when not to use it. When I think I should use it, then I get errors such as in this example (PS. HashPhrase is a class created by me):
using (HashPhrase hash = new HashPhrase())
{
connection.ConnectionString =
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + filePath + ";" +
"Persist Security Info=False;" +
"Jet OLEDB:Database Password=" + hash.ShortHash(pass) + ";";
}
But it gives me an error: 'Password_Manager.HashPhrase': type used in a using statement must be implicitly convertible to 'System.IDisposable'
But in this example it works fine:
using (OleDbConnection connection = new OleDbConnection())
{
connection.ConnectionString =
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + filePath + ";" +
"Persist Security Info=False;" +
"Jet OLEDB:Database Password=" + hash.ShortHash(pass) + ";";
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
Are there any quick guidelines how to determine when using
statement should be used?
Upvotes: 8
Views: 12770
Reputation: 1963
Most of the time I don't check first if a class implements IDisposable
(since this might be buried in a base class), I just code a Using
block if I think it makes sense, and if it fails to compile, I roll back to a standard construction. Lazy? Yeah, a bit. But it can be faster.
Eg, VB:
Using oSerializer As XmlSerializer = New XmlSerializer(GetType(MyType))
...
End Using
Eg, C#:
using (XmlSerializer serializer = new XmlSerializer(typeof(MyType)))
{
...
}
Though I think this should be possible, neither of the above fragments will actually compile because XmlSerializer
doesn't implement IDisposable
(which I find a bit strange to be honest considering that associated classes such as XmlReader
and XmlWriter
do implement IDisposable
).
So then you can just roll back to standard construction.
VB:
Dim oSerializer As XmlSerializer = New XmlSerializer(GetType(MyType))
...
C#:
XmlSerializer serializer = new XmlSerializer(typeof(MyType));
...
I certainly agree with using Using
where it's available; it's clean, concise and you get what's advertised in disposal and clean-up. Nice!
Upvotes: 0
Reputation: 41892
You should use a using
statement whenever the class implements the IDisposable interface.
It's a shorthand for wrapping the object in a try-finally block to ensure the object's Dispose
method is always called to release any resources, regardless of whether an exception is thrown.
To check, right-click the class name in Visual Studio and select Go To Declaration to open it in the Object Browser. You can then check easily whether the class or its base types implement IDisposable.
Upvotes: 5
Reputation: 851
The easiest way is to see if the object implements the IDisposable interface. So, right click the object/class in question, select Go to Definition from the dropdown (or press F12), and see if the class implements IDisposable. Note, in a class that implements many interfaces/other classes, you may need to actually check those classes to see if they implement IDisposable as well.
Upvotes: 4
Reputation: 273314
In
using (var r = new R())
{
// use r
}
the class R
should implement the IDisposing
interface.
Upvotes: 4
Reputation: 269468
Your question already touches on the answer.
If the type implements the IDisposable
interface then you should aim to use using
whenever possible (that is, always, unless there's a compelling reason not to).
And if the type doesn't implement IDisposable
then you can't use using
, and the compiler will tell you so, as you've already discovered.
Upvotes: 16