C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we will explore the goto
statement in C#. The goto
statement is a form of control flow that allows you to unconditionally jump to a labeled statement within the same method. Although the use of goto
is generally discouraged due to its potential to create spaghetti code and make the code harder to understand and maintain, there are cases where it can be useful, such as breaking out of nested loops or simplifying a state machine.
The goto
statement has the following syntax:
goto label;
You need to have a labeled statement in your code where the control flow will jump to:
label: statement;
Here's an example of how to use the goto
statement to jump between labeled statements:
int x = 5; start: if (x > 0) { Console.WriteLine(x); x--; goto start; } Console.WriteLine("Finished counting down.");
In this example, we use a goto
statement to jump to the start
label until x
is no longer greater than 0. The output will be:
5 4 3 2 1 Finished counting down.
One of the legitimate use cases for the goto
statement is breaking out of nested loops. Here's an example:
int[,] matrix = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; bool found = false; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (matrix[i, j] == 5) { found = true; Console.WriteLine($"Found 5 at ({i}, {j})"); goto breakNestedLoops; } } } breakNestedLoops: if (!found) { Console.WriteLine("5 not found."); }
In this example, we use a goto
statement to break out of nested for
loops as soon as we find the number 5 in the matrix.
In most cases, it is better to use structured control flow statements like if
, else
, while
, for
, and foreach
instead of goto
. You can also use methods to encapsulate control flow logic and return
statements to exit early.
For example, the countdown example can be rewritten using a while
loop:
int x = 5; while (x > 0) { Console.WriteLine(x); x--; } Console.WriteLine("Finished counting down.");
This tutorial demonstrates the basics of the goto
statement in C#. While the goto
statement can be useful in some situations, it should be used sparingly and with caution, as it can make your code more difficult to understand and maintain.
How to use goto in C#
Using goto
in C# involves creating a label and jumping to that label using the goto
keyword. Here's a simple example:
using System; class Program { static void Main() { int i = 0; // Define a label start: Console.WriteLine(i); i++; // Use goto to jump to the label if (i < 5) goto start; Console.ReadLine(); } }
C# labeled statements with goto
You can label statements to provide a target for the goto
statement. Here's an example:
using System; class Program { static void Main() { int i = 0; // Define a labeled statement loopStart: Console.WriteLine(i); i++; if (i < 5) goto loopStart; Console.ReadLine(); } }