switch (C# reference)
This article covers the switch statement. For information on the switch expression (introduced in C# 8.0), see the article on switch expressions in the expressions and operators section.
switch is a selection statement that chooses a single switch section to execute from a list of candidates based on a pattern match with the match expression.
The switch statement is often used as an alternative to an if-else construct if a single expression is tested against three or more conditions. For example, the following switch statement determines whether a variable of type Color has one of three values:
It’s equivalent to the following example that uses an if — else construct.
The match expression
The match expression provides the value to match against the patterns in case labels. Its syntax is:
In C# 6 and earlier, the match expression must be an expression that returns a value of the following types:
Starting with C# 7.0, the match expression can be any non-null expression.
The switch section
A switch statement includes one or more switch sections. Each switch section contains one or more case labels (either a case or default label) followed by one or more statements. The switch statement may include at most one default label placed in any switch section. The following example shows a simple switch statement that has three switch sections, each containing two statements. The second switch section contains the case 2: and case 3: labels.
A switch statement can include any number of switch sections, and each section can have one or more case labels, as shown in the following example. However, no two case labels may contain the same expression.
Only one switch section in a switch statement executes. C# doesn’t allow execution to continue from one switch section to the next. Because of this, the following code generates a compiler error, CS0163: «Control cannot fall through from one case label ( ) to another.»
This requirement is usually met by explicitly exiting the switch section by using a break, goto, or return statement. However, the following code is also valid, because it ensures that program control can’t fall through to the default switch section.
Execution of the statement list in the switch section with a case label that matches the match expression begins with the first statement and proceeds through the statement list, typically until a jump statement, such as a break , goto case , goto label , return , or throw , is reached. At that point, control is transferred outside the switch statement or to another case label. A goto statement, if it’s used, must transfer control to a constant label. This restriction is necessary, since attempting to transfer control to a non-constant label can have undesirable side-effects, such transferring control to an unintended location in code or creating an endless loop.
Case labels
Each case label specifies a pattern to compare to the match expression (the caseSwitch variable in the previous examples). If they match, control is transferred to the switch section that contains the first matching case label. If no case label pattern matches the match expression, control is transferred to the section with the default case label, if there’s one. If there’s no default case, no statements in any switch section are executed, and control is transferred outside the switch statement.
For information on the switch statement and pattern matching, see the Pattern matching with the switch statement section.
Because C# 6 supports only the constant pattern and doesn’t allow the repetition of constant values, case labels define mutually exclusive values, and only one pattern can match the match expression. As a result, the order in which case statements appear is unimportant.
In C# 7.0, however, because other patterns are supported, case labels need not define mutually exclusive values, and multiple patterns can match the match expression. Because only the statements in the first switch section that contains the matching pattern are executed, the order in which case statements appear is now important. If C# detects a switch section whose case statement or statements are equivalent to or are subsets of previous statements, it generates a compiler error, CS8120, «The switch case has already been handled by a previous case.»
The following example illustrates a switch statement that uses a variety of non-mutually exclusive patterns. If you move the case 0: switch section so that it’s no longer the first section in the switch statement, C# generates a compiler error because an integer whose value is zero is a subset of all integers, which is the pattern defined by the case int val statement.
You can correct this issue and eliminate the compiler warning in one of two ways:
By changing the order of the switch sections.
By using a when clause in the case label.
The default case
The default case specifies the switch section to execute if the match expression doesn’t match any other case label. If a default case is not present and the match expression doesn’t match any other case label, program flow falls through the switch statement.
The default case can appear in any order in the switch statement. Regardless of its order in the source code, it’s always evaluated last, after all case labels have been evaluated.
Pattern matching with the switch statement
Each case statement defines a pattern that, if it matches the match expression, causes its containing switch section to be executed. All versions of C# support the constant pattern. The remaining patterns are supported beginning with C# 7.0.
Constant pattern
The constant pattern tests whether the match expression equals a specified constant. Its syntax is:
where constant is the value to test for. constant can be any of the following constant expressions:
- A bool literal: either true or false .
- Any integral constant, such as an int , a long , or a byte .
- The name of a declared const variable.
- An enumeration constant.
- A char literal.
- A string literal.
The constant expression is evaluated as follows:
If expr and constant are integral types, the C# equality operator determines whether the expression returns true (that is, whether expr == constant ).
Otherwise, the value of the expression is determined by a call to the static Object.Equals(expr, constant) method.
The following example uses the constant pattern to determine whether a particular date is a weekend, the first day of the work week, the last day of the work week, or the middle of the work week. It evaluates the DateTime.DayOfWeek property of the current day against the members of the DayOfWeek enumeration.
The following example uses the constant pattern to handle user input in a console application that simulates an automatic coffee machine.
Type pattern
The type pattern enables concise type evaluation and conversion. When used with the switch statement to perform pattern matching, it tests whether an expression can be converted to a specified type and, if it can be, casts it to a variable of that type. Its syntax is:
where type is the name of the type to which the result of expr is to be converted, and varname is the object to which the result of expr is converted if the match succeeds. The compile-time type of expr may be a generic type parameter, starting with C# 7.1.
The case expression is true if any of the following is true:
expr is an instance of the same type as type.
expr is an instance of a type that derives from type. In other words, the result of expr can be upcast to an instance of type.
expr has a compile-time type that is a base class of type, and expr has a runtime type that is type or is derived from type. The compile-time type of a variable is the variable’s type as defined in its type declaration. The runtime type of a variable is the type of the instance that is assigned to that variable.
expr is an instance of a type that implements the type interface.
If the case expression is true, varname is definitely assigned and has local scope within the switch section only.
Note that null doesn’t match a type. To match a null , you use the following case label:
The following example uses the type pattern to provide information about various kinds of collection types.
Instead of object , you could make a generic method, using the type of the collection as the type parameter, as shown in the following code:
The generic version is different than the first sample in two ways. First, you can’t use the null case. You can’t use any constant case because the compiler can’t convert any arbitrary type T to any type other than object . What had been the default case now tests for a non-null object . That means the default case tests only for null .
Without pattern matching, this code might be written as follows. The use of type pattern matching produces more compact, readable code by eliminating the need to test whether the result of a conversion is a null or to perform repeated casts.
The case statement and the when clause
Starting with C# 7.0, because case statements need not be mutually exclusive, you can add a when clause to specify an additional condition that must be satisfied for the case statement to evaluate to true. The when clause can be any expression that returns a Boolean value.
The following example defines a base Shape class, a Rectangle class that derives from Shape , and a Square class that derives from Rectangle . It uses the when clause to ensure that the ShowShapeInfo treats a Rectangle object that has been assigned equal lengths and widths as a Square even if it hasn’t been instantiated as a Square object. The method doesn’t attempt to display information either about an object that is null or a shape whose area is zero.
Note that the when clause in the example that attempts to test whether a Shape object is null doesn’t execute. The correct type pattern to test for a null is case null: .
C# language specification
For more information, see The switch statement in the C# Language Specification. The language specification is the definitive source for C# syntax and usage.
C Урок 10. Оператор switch
Очень часто при составлении исходного кода будущей программы мы сталкиваемся с ситуацией, когда дальнейший ход нашей программы зависит от конкретного значения какой-то переменной либо выражения, причём когда выбор таких значений, мягко говоря, немаленький. Мы, конечно же в данном случае можем воспользоваться условными конструкциями if, else, else if, но в данном случае это будет не совсем удобно и читабельно. Для этой ситуации существует такая конструкция, чтобы было очевидно, что мы выбираем какие-то конкретные варианты значений одной переменной или одного выражения.
И таким оператором и служит switch, который также можно отнести к оператору ветвления. Также данный оператор очень часто называют оператором вариантов. Ещё в народе его зовут переключателем в соответствии с его переводом на русский.
switch – оператор, который сравнивает последовательно значение переменной, находящейся в скобках со всеми вариантами значений, находящимися после каждого ключевого слова case. При совпадении данных значений выполняется код, следующий за case. Затем, если встретится ключевое слово break либо закрывающая фигурная скобка, произойдет выход из конструкции switch. Если со значением переменной не совпадет ни одно из значений, выполнится код после необязательной инструкции default
Оператор break в ветвях case является необязательным. Если его не будет в какой-то из ветвей, то выхода из тела оператора не произойдёт и код продолжит выполнение дальше внутри тела. Причём, если дальше встретится следующая ветвь case, то её код выполнится независимо от сравниваемого значения. Приём без break используется как правило для того, чтобы выполнить ту или иную ветвь в зависимости не от одного, а от нескольких вариантов значения переменной (выражения), находящейся(гося) в скобках. Выглядеть такая ветвь будет примерно вот так
Ветвь default, как было указано выше, также необязательна и при её отсутствии в случае несовпадения значения переменной или выражения ни с одним значением в вариантах ничего не делается, то есть ни один участок кода, находящийся в теле оператора switch, не будет выполнен.
Также нелишним будет заметить что ветви case и default можно располагать в любом порядке, хотя мы привыкли, что ветвь default, которая, кстати быть должна только одна, всегда в конструкции switch расположена в самом низу тела.
Если вы вдруг что-то не поняли или недопоняли из объяснения выше, то, надеюсь, смысл оператора switch будет понятен из практической части. Также данным оператором в будущих уроках мы будем очень часто пользоваться и вы непременно к нему привыкнете.
И давайте теперь перейдём к нашей практической части. Я постараюсь дать такой простой пример, который будет понятен каждому. Пусть он не пригодится в будущем, но сейчас самое важное для нас то, чтобы мы уяснили, как именно работает конструкция switch.
Создадим новый проект из проекта прошлого занятия с именем MYPROG09 и присвоим ему имя MYPROG10.
Откроем файл main.c и в функции main(), как обычно, удалим весь код тела кроме возврата нуля, останется от него вот это
int main()
return 0 ; //Return an integer from a function
Добавим код, в котором программа попросит пользователя ввести целое число из предложенных вариантов. Затем мы введённое пользователем число с помощью оператора switch, с которым мы познакомились выше, обработаем соответствующим образом. В свою очередь, ввод числа и конструкцию switch мы также обернём в бесконечный цикл, чтобы нас после каждого ввода числа не выбрасывало из программы