setjmp/longjmp in C#. Is it possible? -


i ran problem when need goto local scope:

if(...)       {    dosomethinghere();    if (...) goto label; } else if(...) { label:   dosomethingheretoo(); } 

, apparently not possible in c#.

yes know using goto considered bad practice, in case easier goto. i'd rather not whole "goto's source of evil" discussion. me more interesting , more general question possibility of setjmp/longjmp in c#. @ possible?

first off, think confusing doing "goto" local scope - short jump - long jump - doing goto place entirely outside of current method. classic c-style long jump can thought of in 2 ways: one, it's throwing exception not clean stack frames. two, returning function "wrong" address.

none of above possible in c#. c# not support long jumps; have try-catch-finally-throw non-local gotos in clean, structured , safe way.

c# not support short jumps outside local variable declaration space inside space. reason because jumping middle of block outside confusing, dangerous, hard understand , hard maintain. way design goal accomplished making labels have same scope local variables. "goto" doesn't see label, more code in location see local variable declared in different local variable declaration space.

there plenty of ways solve problem without using goto statements @ all. example, 1 comes mind is

bool dofirstthing = false; bool dosecondthing = false; if (firstcondition)  {     dofirstthing = true;     dosecondthing = true; } else if (secondcondition) {     dosecondthing = true; } if (dofirstthing)  {     dofirstthing(); } if (dosecondthing) {     dosecondthing(); } 

that's straightforward, easy read, easy debug, , on.

alternatively: if shared code in "dosecondthing" consequence difficult refactor method of own consider taking step , deciding whether control flow complicated begin with. example, if mutating lots of variables in loop perhaps there technique can use simplify situation , fewer mutations. can give more information code doing , why difficult refactor it?


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -