Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Valid way to use using
Message
De
21/11/2008 13:54:48
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP SP2
Application:
Desktop
Divers
Thread ID:
01363486
Message ID:
01363543
Vues:
14
>>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
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform