Call Close in Using Block with StreamWriter

  • Thread starter Thread starter JohnMSyrasoft
  • Start date Start date
J

JohnMSyrasoft

I'm coding in VB.NET 2005 with a Using block and a StreamWriter as in

Using strWrite As New IO.StreamWriter(fileName, True)
strWrite.WriteLine(Now & vbTab & cLineToWrite)
End Using

Should I explicitly call the Close method on the StreamWriter or is the
object closed and disposed when the Using block is exited?

Thanks for your insights.
 
I'm coding in VB.NET 2005 with a Using block and a StreamWriter as in

Using strWrite As New IO.StreamWriter(fileName, True)
strWrite.WriteLine(Now & vbTab & cLineToWrite)
End Using

Should I explicitly call the Close method on the StreamWriter or is the
object closed and disposed when the Using block is exited?

Thanks for your insights.

As far as i know, "End Using" statement is enough to dispose resource.
No need to use close method in that case.
 
I'm coding in VB.NET 2005 with a Using block and a StreamWriter as in

Using strWrite As New IO.StreamWriter(fileName, True)
strWrite.WriteLine(Now & vbTab & cLineToWrite)
End Using

Should I explicitly call the Close method on the StreamWriter or is the
object closed and disposed when the Using block is exited?

Thanks for your insights.

It is closed when the using block exits - even if there is an error,
that is the point of a using block. Calling close explicitly, just makes
for redundant code.
 
Tom Shelton said:
Calling close explicitly, just makes for redundant code.

Not necessarially. You may be done using the object, but not have hit the
"End Using" yet. With a resource like a file (that others may need to
access), it may be a good idea to call close() explicitly as soon as you are
done using it and use "End Using" as a fail-safe.

-Scott
 
Not necessarially. You may be done using the object, but not have hit the
"End Using" yet. With a resource like a file (that others may need to
access), it may be a good idea to call close() explicitly as soon as you are
done using it and use "End Using" as a fail-safe.

-Scott

IMHO, that would be a rare case, and one that I would suggest is
probably in need of refactoring. In general, with files the rule is
get in, get what you need, and get out. No, in general, I would stand
by my original post.
 
IMHO, I don't think it's as rare as you may think. If I'm going to need to
re-open the stream after I close it, I wouldn't want it disposed yet, but I
would want it closed while I'm not using it.
 
IMHO, I don't think it's as rare as you may think.  If I'm going to needto
re-open the stream after I close it, I wouldn't want it disposed yet, but I
would want it closed while I'm not using it.

On a stream, Close = Dispose. If you call close, then you have to
reopen it anyway. In case you doubt me, here is the code for Stream's
close method:

Public Overridable Sub Close()
Me.Dispose(True)
GC.SuppressFinalize(Me)
End Sub

It is inherited by all of the Stream derived classes.

Again, I respectfully disagree with you on this.
 
Back
Top