You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please do excuse me, if I am not up-to-date concerning thelocal functions-feature, but I have the following question/proposal:
Should it be possible, to allow a local function to return from the "parent" function?
I mean something along these lines:
publicvoidProcess( ...){SomeTypeobj1= ...;SomeTypeobj2= ...;// some resource-intensive objectsvoidcleanup(){// do some object clean up
obj1.Close();
obj2.CleanUp();
obj2.Dispose();returnreturn;// return from "parent"}// main logic begins here
...if(!some_condition_1)
cleanup();
...if(!some_condition_2)
cleanup();
...}
which would be identical to:
publicvoidProcess( ...){SomeTypeobj1= ...;SomeTypeobj2= ...;// some resource-intensive objectsvoidcleanup(){// do some object clean up
obj1.Close();
obj2.CleanUp();
obj2.Dispose();}// main logic begins here
...if(!some_condition_1){
cleanup();return;}
...if(!some_condition_2){
cleanup();return;}
...}
As you can see, the statement return return; would not only return from the local function, but also from the parent one. (the syntax or keyword does not really matter to me --- it is more about the point of being able to emit control-statements to the parent function)
The text was updated successfully, but these errors were encountered:
Yup, Neal's comment is still relevant: local functions are lowered into their own methods, so they can't interact with the parent method except through the standard inter-method control flow (e.g., returning to the caller, yielding to the caller, etc.).
Since we're closing down for language features now I'm going to say that even if we liked this feature we wouldn't have time to put it into C# 7.
However, if you really want this feature and want it in a future version of C#, you could make a proposal for language design. For now I'll close this issue, though.
Please do excuse me, if I am not up-to-date concerning the
local functions
-feature, but I have the following question/proposal:Should it be possible, to allow a local function to return from the "parent" function?
I mean something along these lines:
which would be identical to:
As you can see, the statement
return return;
would not only return from the local function, but also from the parent one. (the syntax or keyword does not really matter to me --- it is more about the point of being able to emit control-statements to the parent function)The text was updated successfully, but these errors were encountered: