Click here to Skip to main content
16,015,018 members
Home / Discussions / C#
   

C#

 
AnswerRe: when is finally?! Pin
dan!sh 14-Nov-10 5:10
professional dan!sh 14-Nov-10 5:10 
GeneralRe: when is finally?! Pin
Pete O'Hanlon14-Nov-10 9:00
mvePete O'Hanlon14-Nov-10 9:00 
GeneralRe: when is finally?! Pin
PIEBALDconsult14-Nov-10 11:30
mvePIEBALDconsult14-Nov-10 11:30 
AnswerRe: when is finally?! Pin
Henry Minute14-Nov-10 5:23
Henry Minute14-Nov-10 5:23 
AnswerRe: when is finally?! Pin
Luc Pattyn14-Nov-10 7:08
sitebuilderLuc Pattyn14-Nov-10 7:08 
GeneralRe: when is finally?! Pin
Henry Minute14-Nov-10 7:54
Henry Minute14-Nov-10 7:54 
GeneralRe: when is finally?! Pin
Jacob D Dixon14-Nov-10 14:09
Jacob D Dixon14-Nov-10 14:09 
GeneralRe: when is finally?! [modified] Pin
Henry Minute14-Nov-10 14:20
Henry Minute14-Nov-10 14:20 
Please understand that I am by no means an expert in this regard (well, almost any regard, actually Smile | :) ) but my understanding is that it does the equivalent of a nested try-finally block, whether the IL is identical I'm not sure, so that the resource are released earlier rather than waiting for the main t-c-f block to end. I think that anything that avoids nested t-c-f is preferable as they are difficult to read and just plain ugly.

From the MSDN page

The using statement allows the programmer to specify when objects that use resources should release them.


Also from MSDN

A using statement can be exited either when the end of the using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.


I have always taken this to mean that disposal will take place even if one of the Exceptions mentioned by PO'H above occurs. It is always possible that I am wrong on that count though.

[Edit]
I have just found this[^] article which, would seem to confirm your assertion that using generates a try-finally in the IL code. So the only real benefit would be tighter control over when resources are disposed and slightly neater, IMO, code.
[/Edit]
Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
modified on Sunday, November 14, 2010 8:47 PM

GeneralRe: when is finally?! Pin
Jeff Connelly15-Nov-10 4:38
Jeff Connelly15-Nov-10 4:38 
GeneralRe: when is finally?! Pin
Jacob D Dixon15-Nov-10 10:34
Jacob D Dixon15-Nov-10 10:34 
GeneralRe: when is finally?! Pin
Jeff Connelly15-Nov-10 10:49
Jeff Connelly15-Nov-10 10:49 
Questionpixel related question Pin
inayathussaintoori13-Nov-10 4:48
inayathussaintoori13-Nov-10 4:48 
AnswerRe: pixel related question Pin
Abhinav S13-Nov-10 7:01
Abhinav S13-Nov-10 7:01 
GeneralRe: pixel related question Pin
inayathussaintoori13-Nov-10 7:15
inayathussaintoori13-Nov-10 7:15 
QuestionPlay multiple sounds with SoundPlayer and SDK DirectSound Pin
Mc_Topaz13-Nov-10 0:57
Mc_Topaz13-Nov-10 0:57 
Questiongiving authorization to the user Pin
Erdinc2712-Nov-10 23:55
Erdinc2712-Nov-10 23:55 
AnswerRe: giving authorization to the user Pin
Eddy Vluggen13-Nov-10 9:01
professionalEddy Vluggen13-Nov-10 9:01 
GeneralRe: giving authorization to the user Pin
Erdinc2713-Nov-10 9:50
Erdinc2713-Nov-10 9:50 
GeneralRe: giving authorization to the user Pin
Eddy Vluggen13-Nov-10 11:21
professionalEddy Vluggen13-Nov-10 11:21 
QuestionSkins Pin
Anil Kumar.Arvapalli12-Nov-10 18:45
Anil Kumar.Arvapalli12-Nov-10 18:45 
AnswerRe: Skins Pin
thatraja12-Nov-10 19:11
professionalthatraja12-Nov-10 19:11 
AnswerRe: Skins Pin
Kanasz Robert13-Nov-10 6:02
professionalKanasz Robert13-Nov-10 6:02 
GeneralRe: Skins Pin
Anil Kumar.Arvapalli13-Nov-10 23:19
Anil Kumar.Arvapalli13-Nov-10 23:19 
QuestionUse Automation To Open Solution Pin
Kevin Marois12-Nov-10 11:36
professionalKevin Marois12-Nov-10 11:36 
AnswerRe: Use Automation To Open Solution Pin
Eddy Vluggen13-Nov-10 8:59
professionalEddy Vluggen13-Nov-10 8:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.