C# Goto Statement
Syntax
goto label;
Example
start:
Console.WriteLine("Hello");
goto start;
Output
Hello
Hello
Hello
...
Explanation
The goto
statement in C# is used to transfer the control to a label within the same block, method, or switch statement.
The label must be defined before the goto
statement. The code execution will jump to the line immediately after the label and continue executing from that point onwards.
The use of goto
statement is generally considered bad programming practice because it can make the code difficult to read and understand.
Use
The goto
statement is rarely used in C# programming because it can make the code difficult to understand. However, there may be situations where it can be useful.
One such situation can be in error handling. For example, you may want to jump to a specific label if an error occurs and handle the error at that label.
Important Points
- The
goto
statement is used to transfer control to a label within the same block, method, or switch statement. - The label must be defined before the
goto
statement. - The use of
goto
statement can make the code difficult to read and understand. - The
goto
statement is rarely used in C# programming.
Summary
The goto
statement is rarely used in C# programming because it can make the code difficult to understand. It is used to transfer control to a label within the same block, method, or switch statement. The label must be defined before the goto
statement. Although there may be situations where it can be useful, it is generally considered bad programming practice.