Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Valid way to use using
Message
From
21/11/2008 13:54:48
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Application:
Desktop
Miscellaneous
Thread ID:
01363486
Message ID:
01363543
Views:
12
>>Hi All,
>>
>>Just checking, is this a valid way to use the using statement in this case? Is the StreamWriter closed and disposed?
>
>Yes. A "using" statement ends up being compiled as (essentially, not exactly):
>
>
>try
>{
>   stream = File.Open(deleteListPath, FileMode.Open)
>}
>finally
>{
>   stream.Dispose();
>}
>
>
>I also just took a look at the Stream base class inside of Reflector and Close() basically does this:
>
>
>this.Dispose(true);
>GC.SuppressFinalize(this);
>
>
>So a call to both Close and Dispose turns out to not be necessary. One or the other is fine.

Perfect that answered two questions in one. What I found interesting was I had to define the FileStream as null first.
I had to do this.
FileStream stream = null
using (stream = File.Open(deleteListPath, FileMode.Open))

I first tried to do this but it wouldn't work.:
using(FileStream stream = File.Open(deleteListPath, FileMode.Open))

Tim
Timothy Bryan
Previous
Reply
Map
View

Click here to load this message in the networking platform