Any time you instantiate a class that Implements IDisposable
, you should call .Dispose
1 on that class when you have finished using it. This allows the class to clean up any managed or unmanaged dependencies that it may be using. Not doing this could cause a memory leak.
The Using
keyword ensures that .Dispose
is called, without you having to explicitly call it.
For example without Using
:
Dim sr As New StreamReader("C:\foo.txt")
Dim line = sr.ReadLine
sr.Dispose()
Now with Using
:
Using sr As New StreamReader("C:\foo.txt")
Dim line = sr.ReadLine
End Using '.Dispose is called here for you
One major advantage Using
has is when an exception is thrown, because it ensures .Dispose
is called.
Consider the following. If an exception is thrown, you need to need to remember to call .Dispose but you might also have to check the state of the object to ensure you don't get a null reference error, etc.
Dim sr As StreamReader = Nothing
Try
sr = New StreamReader("C:\foo.txt")
Dim line = sr.ReadLine
Catch ex As Exception
'Handle the Exception
Finally
If sr IsNot Nothing Then sr.Dispose()
End Try
A using block means you don't have to remember to do this and you can declare your object inside the try
:
Try
Using sr As New StreamReader("C:\foo.txt")
Dim line = sr.ReadLine
End Using
Catch ex As Exception
'sr is disposed at this point
End Try
1 Do I always have to call Dispose() on my DbContext objects? Nope