Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5 C# .NET4
private void SomeFunction()
{
    using (StreamReader sr = new StreamReader("C:\\File.txt"))
    {
        if (someCondition)
        {
            return;
        }
    }
}
 
Maybe a stupid question...
But does the file still get closed given that the return statement jumps out of the function?
Posted 27-Feb-13 10:57am
Comments
richcb at 27-Feb-13 15:59pm
   
Well, your function is not set to return anything, but I am not sure on the file getting closed. I think it is still good practice to manually close it.
Sergey Alexandrovich Kryukov at 27-Feb-13 16:12pm
   
You should know such thing, as it is very important! Of course it will be closed (more exactly IDispoable.Dispose is automatically called) Please see my answer.
See also "using statement" on MDSN.
—SA
Sergey Alexandrovich Kryukov at 27-Feb-13 16:16pm
   
By the way, OP could easily just check it up, with throwing exception and without it. It's a good idea to always try key techniques.
—SA
richcb at 27-Feb-13 17:25pm
   
Ok, I will take your advice, thank you.
Sergey Alexandrovich Kryukov at 27-Feb-13 17:31pm
   
You are very welcome. I don't know if you saw the most recent version of my answer; I've added to it 3 times; but right now it's pretty much comprehensively explains everything.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Of course, it will work properly.
 
[EDIT]
 
The use of using is strictly equivalent to analogous try-finally statement with sr.Dispose() in the finally block. The using statement is just a syntax sugar (http://en.wikipedia.org/wiki/Syntax_sugar[^]).
 
private void SomeFunction()
{
    StreamReader sr = new StreamReader("C:\\File.txt");
    try {
        if (someCondition)
            return;
    } finally {
       sr.Dispose(); // same happens "on the closing '}' of using statement"
    }
}
 
Please see: http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.80%29.aspx[^].
 
—SA
  Permalink  
v4
Comments
DanielSheets at 27-Feb-13 18:07pm
   
Thanks! For some reason my mind is telling me that the closing brace is never reached because of the return statement.
 
Noobish... I know.
Sergey Alexandrovich Kryukov at 27-Feb-13 18:11pm
   
You are welcome.
And it's perfectly all right: if you are in doubt, you should always check up things, I appreciate it. Only you could easily check it by yourself; in future, please always try by yourself first.
Good luck, call again.
—SA
Sergey Alexandrovich Kryukov at 27-Feb-13 18:14pm
   
As to "never reached"... this is a typical confusion of try-finally, exceptions in general and "using" statement. Look at it this way: '}' is not a statement, if cannot be "reached". It's only end of context, not a sequence of any CPU or CIL instructions. If you look at decompiled code, you won't see any instructions associated to '}'. If you look this way, some weirdness you might see right now should disappear.
—SA
DanielSheets at 27-Feb-13 18:25pm
   
Great explanation! And you're right... I never thought of it that way. That does take some of the mud out of the water.
Sergey Alexandrovich Kryukov at 27-Feb-13 18:29pm
   
I hope so. Thank you, Daniel.
Good luck,
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
Yes,the file will be closed, and all the resources inside the using statement will be disposed regardless of what happens.
 
See microsoft article :
http://msdn.microsoft.com/en-gb/library/yh598w02.aspx[^]
 
Regards
Jegan
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Yes. Use ILdasm to examine the IL produced. Hint: a 'finally' clause is generated that includes the code to Dispose() of your reader.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 BillWoodruff 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100