Case windows form c

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.

Читайте также:  Как переустанавливать лицензионный windows 10

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:

Читайте также:  Рейтинг лучших сборок windows 2019 2020

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.

ez code

Просто о сложном.

Конструкция switch — case в C++

Сегодня мы научимся пользоваться этой полезной конструкцией языка c++.

Очень часто в процессе написания программы требуется писать длинные if-else конструкции, например, когда мы получаем какой-либо ключ от пользователя; если вы пишете игру, то придется проверять на какую кнопку нажал игрок (вправо, влево, пробел и т.д.).

Читайте также:  Как собрать свой дистрибутив windows

В этой статье мы узнаем как удобно оформлять подобные конструкции с помощью switch case, а так же узнаем немного о enum типах, которые хорошо подходят для работы со switch case.

Конструкция switch-case — это удобная замена длинной if-else конструкции, которая сравнивает переменную с несколькими константными значениями, например int или char.

Синтаксис

Переменная в скобках сравнивается со значениями, описанными после ключевого слова case. После двоеточия находится код, который будет выполнен в случае если переменная оказалась равной текущему значению. break необходим для того, чтобы прервать выполнение switch. Рассмотрим пример, где нет break:

Данная программа выведет a = 4.

Значения для сравнения, описанные после case могут быть только константами, поэтому следующий вариант использования switch-case — неверен:

При попытке скомпилировать данную программу, вы получите подобное сообщение:

Блок default — необязателен, но он полезен для обработки исключительных ситуации.

Следующая программа демонстрирует один из возможных вариантов использования switch-case:

Эта программа показывает простой способ обработки вводимых пользователем данных.

Минус данной программы в том, что она дает только одну попытку, без права на ошибку 🙂 . Это легко исправить, заключив весь блок switch-case в цикл. Но может возникнуть вопрос: внутри switch-case есть break, не прервет ли он выполнение цикла? Нет, break прервет только выполнение блока switch.

Сравнение switch-case с if-else

Если у вас возникают проблемы с пониманием того, как работает switch-case, посмотрите на следующую if-else конструкцию, она работает точно так же, как и switch

Если мы можем сделать то же самое с помощью if-else, зачем вообще нужен switch? Главное преимущество этой конструкции в том, что нам понятно, как работает программа: единственная переменная контролирует поведение программы. В случае с if-else, придется внимательно читать каждое условие.

Создаем собственные типы с помощью enumeration

Иногда при написании программ могут понадобится переменные, которые могут принимать только строго определенные значения, которые известны вам заранее. Например, вы можете задать ограниченный набор цветов, которые пользователь может выбрать. Очень удобно иметь сразу и набор доступных констант и тип переменной, который связан с этим набором. Более того, подобные переменные хорошо подходят для использования в switch-case, так как вы знаете все возможные значения заранее.

Тип enum (сокращение от «enumerated type«, перечисляемые типы) содержит перечисление различных значений, например цветов радуги:

Несколько важных моментов:

  • Для объявление перечисляемого типа используйте ключевое слово enum.
  • Имя нового типа вы задаете сами, например RainbowColor.
  • Все возможные значения для данного типа перечислены в фигурных скобках.
  • После объявления перечисления необходима точка с запятой.

Теперь вы можете объявлять переменные с типом RainbowColor:

И, как уже говорилось, эти переменные хорошо подходят для использования в switch:

Так как мы используем перечисления, мы можем быть уверенными, что рассмотрели все возможные значения переменной. Значения констант в перечислении — это простой int, по умолчанию каждое следующее значение больше предыдущего на 1. Для первого — 0, для второго — 1 и т.д. В нашем случае: RC_RED = 0 и RC_ORANGE = 1.

Вы также можете задать собственные значения:

Преимущество использования перечисляемых типов в том, что вы можете задать имя значениям, которые иначе пришлось бы хард-кодить. Например, если вы пишете игру крестики-нолики, вам нужен способ представления крестиков и ноликов на доске. Это может быть 0 для пустой клетки, 1 для нолика и 2 для крестика. Значит, наверняка придется использовать подобный код:

Данный код очень сложен для понимания и обслуживания, потому что совсем не понятно, что означает цифра 1. Enum типы позволяют избегать таких ситуаций:

Теперь все гораздо понятнее 🙂 .

На этом всё! Подписывайтесь и не пропустите новые уроки! 🙂

Оцените статью