Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

The goto-less goto!

0.00/5 (No votes)
3 Feb 2011CPOL 6.9K  
In C#, you can always use try..finally to add some finalization code.Just write:bool bFailed = true; try{ if(condition1_fails) return; ... if(condition2_fails) return; ... ... if(conditionN_fails) ...
In C#, you can always use try..finally to add some finalization code.
Just write:

C#
bool bFailed = true;  
try
{
     if(condition1_fails)     
          return;  
     ...  
     if(condition2_fails)     
          return;  
     ...  
     ...  
     if(conditionN_fails)     
          return;  
     bFailed=false;  
     PerformActionOnAllSuccess();
}
finally
{
     if(bFailed)     
          DoFailedCleanup();  
     else     
          DoNormalCleanup();
}


But I don't think it's a good practice.
We shouldn't use goto not because it's an ugly word. We shouldn't use goto because its behavior implies poor code readability, and all alternatives that just change the words, but not the bad behavior, should be avoided.

License

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