Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there ever a reason to use goto in modern .NET code?

Tags:

c#

.net

goto

I just found this code in reflector in the .NET base libraries...

    if (this._PasswordStrengthRegularExpression != null)     {         this._PasswordStrengthRegularExpression = this._PasswordStrengthRegularExpression.Trim();         if (this._PasswordStrengthRegularExpression.Length == 0)         {             goto Label_016C;         }         try         {             new Regex(this._PasswordStrengthRegularExpression);             goto Label_016C;         }         catch (ArgumentException exception)         {             throw new ProviderException(exception.Message, exception);         }     }     this._PasswordStrengthRegularExpression = string.Empty; Label_016C:     ... //Other stuff 

I've heard all of the "thou shalt not use goto on fear of exile to hell for eternity" spiel. I always held MS coders in fairly high regard and while I may not have agreed with all of their decisions, I always respected their reasoning.

So - is there a good reason for code like this that I'm missing? Was this code extract just put together by an inept developer? or is .NET reflector returning inaccurate code?

I'm hoping there is a good reason, and I'm just blindly missing it.

Thanks for everyone's input

like image 753
BenAlabaster Avatar asked Mar 30 '10 01:03

BenAlabaster


People also ask

Is there ever a reason to use goto?

In fact, IDL's own documentation advises against it. Actually, it doesn't advise against it; it outright states that using it is bad programming: "The GOTO statement is generally considered to be a poor programming practice that leads to unwieldy programs. Its use should be avoided."

Should we use goto C#?

It is used to transfer control to the other part of the program. It unconditionally jumps to the specified label. It can be used to transfer control from deeply nested loop or switch case label. Currently, it is avoided to use goto statement in C# because it makes the program complex.

What can I use instead of goto in C#?

Typically “goto start” can be replaced by “while(true){}”. For example, if you're reading user input, it can be placed into “while(true){}” loop with “break” when the program receives “exit” command.

Under what conditions might the goto statement be helpful?

The goto statement is a jump statement which is sometimes also referred to as unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function.


1 Answers

Reflector is not perfect. The actual code of this method is available from the Reference Source. It is located in ndp\fx\src\xsp\system\web\security\admembershipprovider.cs:

        if( passwordStrengthRegularExpression != null )         {              passwordStrengthRegularExpression = passwordStrengthRegularExpression.Trim();             if( passwordStrengthRegularExpression.Length != 0 )              {                  try                 {                      Regex regex = new Regex( passwordStrengthRegularExpression );                 }                 catch( ArgumentException e )                 {                      throw new ProviderException( e.Message, e );                 }              }          }         else          {             passwordStrengthRegularExpression = string.Empty;         } 

Note how it failed to detect the last else clause and compensated for it with a goto. It is almost certainly tripped-up by the try/catch blocks inside the if() statements.

Clearly you'll want to favor the actual source code instead of the decompiled version. The comments in themselves are quite helpful and you can count on the source being accurate. Well, mostly accurate, there's some minor damage from a buggy post-processing tool that removed the names of the Microsoft programmers. Identifiers are sometimes replaced by dashes and the code is repeated twice. You can download the source here.

like image 106
Hans Passant Avatar answered Sep 24 '22 15:09

Hans Passant