GOTO statement in C#.NET

12,681

Solution 1

It won't compile because of 'GOTO block' instead of 'goto block;' and even if it was right it won't compile because C# specifications states (§8.9.3):

The target of a goto identifier statement is the labeled statement with the given label. If a label with the given name does not exist in the current function member, or if the goto statement is not within the scope of the label, a compile-time error occurs. This rule permits the use of a goto statement to transfer control out of a nested scope, but not into a nested scope.

Also I found couple specs interesting for me too:

A goto statement cannot exit a finally block (§8.10). When a goto statement occurs within a finally block, the target of the goto statement must be within the same finally block, or otherwise a compile-time error occurs.

And:

A goto statement is executed as follows:

  • If the goto statement exits one or more try blocks with associated finally blocks, >control is initially transferred to the finally block of the innermost try statement. When >and if control reaches the end point of a finally block, control is transferred to the >finally block of the next enclosing try statement. This process is repeated until the >finally blocks of all intervening try statements have been executed.
  • Control is transferred to the target of the goto statement.

the latter means that if you have

try
{
    ...
    goto Label1;
}
finally
{
    CloseAll();
}

Label1:
   MethodB();

it will call CloseAll() before actually transferring control to the Label1 and executing MethodB().

Makes perfect sense, but I never thought about it...

Solution 2

No. block: label must be outside the try in order for the goto to see it. The code won't even compile. Are you taking a test :)?

Share:
12,681

Related videos on Youtube

Kiran Thokal
Author by

Kiran Thokal

• 11 years of industry experience in the field of application design, development, maintenance and have worked in product development using Agile / scrum methodology etc • Expertise in development using C#, VB.NET, WPF, Web API, Azure, Docker, WCF, MVVM, WinForms, SQL, Win32 etc. • Expertise in development of Desktop applications, Enterprise Applications, AddIns etc. • Exposure to design patterns such as MVVM, IOC/DI and GOF patterns like Singleton, Factory, Adapter, Observer, Repository etc. • Exposure to Builds, Continuous Integration with TFS/VNext Builds, installers etc. • Exposure to Unit Testing using MS Unit and UI Automation testing using White framework and Speckflow. • Having good knowledge of Design principles and OOPs and participate in architecture, design and code reviews etc. • Effective in working independently and collaboratively in teams with minimal supervision. Strong at research and development. Handled maintenance activity. Strong in debugging and issue fixing.

Updated on June 04, 2022

Comments

  • Kiran Thokal
    Kiran Thokal over 1 year
    try
    {...
    block:
    ....
    }
    catch{ ..}
    GOTO block
    ...
    ....
    finally{...}
    

    Will goto executes in this case??

    • ironic
      ironic almost 14 years
      Isn't it a compilation error?
    • apaderno
      apaderno almost 14 years
      Maybe it should be called "spaghetti.NET". ;-)
    • Andreas Bonini
      Andreas Bonini almost 14 years
      Why the downvotes? Yes maybe he should have tried to compile it but still.. I don't think they are deserved
  • Fortega
    Fortega almost 14 years
    the goto is not occuring within the finally block
  • Kiran Thokal
    Kiran Thokal almost 14 years
    @Regent Actually i had not concentrated on syntax.. I get what i want from your answer Thanks