C# Goto Switch Usage

Goto keyword

Goto is used inside switch. This can enhance performance and reduce code size in some cases. There is a special syntax for this construct: it requires goto case or goto default.

This C# example program demonstrates the goto statement in switch.

Example

First we see how you can use goto in a switch statement. This example was written just as an example, but we can make useful observations with it. The output of the program execution is shown after the program text.

Program that uses goto statement in switch [C#]

using System;

class Program
{
    static void Main()
    {
	Console.WriteLine(GetPrice(1000));
	Console.WriteLine(GetPrice(-1));
	Console.WriteLine(GetPrice(int.Parse("100")));
    }

    static int GetPrice(int id)
    {
	//
	// Default price is 5.
	//
	int price = 5;
	//
	// When id is 1000, add 10 to price before multiplying.
	//
	switch (id)
	{
	    case 1000:
		price += 10; // <-- Could get from another method
		goto case 100;
	    case 100:
		return price * 10;
	    default:
		return price;
	}
	//
	// 1.
	// First, if ID is 1000, add ten to default price.
	//
	// 2.
	// If ID is 1000 or 100, multiply price by 10 and return it.
	//
	// 3.
	// If ID is anything else, return price of 5.
	//
    }
}

Output

150
5
50
Steps

Description. Here we see a custom method called GetPrice defined. You will often want to use switches in single methods like this. The GetPrice method performs three logical steps, all defined in the switch statement.

First step of the example. The method receives the ID of an item. If the ID is equal to 1000, the base price is increased by 10. After this occurs, this ID is treated like a price of 100, meaning it goes to that case.

Next steps. For IDs of 1000 or 100, the value returned from the method is equal to the base price times 10. This occurs after IDs of 1000 were processed. For all other values, the price that is returned is the base price, which is 5. You can see how the switch statement has complex control flow.

Goto benefits

Programming tip

Almost always, good C# code is structured into discrete methods, meaning you can simply call the same method with different values from a switch. However, in certain cases you may benefit from goto in switch if you are trying to reduce method size.

Method Size Test

Writing spaghetti code. I don't object to goto statements, but they must be used in a disciplined and well-commented style. Additionally, they may not be optimized as well as structured styles.

Goto Loop Example Spaghetti Code

Reminder on jump tables. The greatest benefit of switches on ints or non-string constants here is that the low-level MSIL jump statement is used. This has significant performance benefits.

Switch Enum

Summary

The C# programming language

We saw how you can use the goto statement in a switch. Because the emphasis of the C# language is on object-oriented programming, this construct is not used heavily in the language. However, it enables you to use the optimized jump table instructions with more complex, layered logic.

Switch Statement
.NET