Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
C#
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
Comments
Richard C Bishop 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 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 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
Richard C Bishop 27-Feb-13 17:25pm    
Ok, I will take your advice, thank you.
Sergey Alexandrovich Kryukov 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

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[^]).

C#
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
 
Share this answer
 
v4
Comments
DanielSheets 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 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 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 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 27-Feb-13 18:29pm    
I hope so. Thank you, Daniel.
Good luck,
—SA
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
 
Share this answer
 
Yes. Use ILdasm to examine the IL produced. Hint: a 'finally' clause is generated that includes the code to Dispose() of your reader.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900