You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a *switch expression conversion* (§switch-expression-conversion) from a switch expression to a type `T`
3903
+
if there is an implicit conversion from every *switch_expression_arm_expression* of each of the switch expression's *switch_expression_arm*s to `T`.
3904
+
3905
+
If a switch expression is not subject to a *switch expression conversion*, then
3906
+
3907
+
- The type of the *switch_expression* is the best common type [§12.6.3.16](expressions.md#126316-finding-the-best-common-type-of-a-set-of-expressions)) of the *switch_expression_arm_expression*s of the *switch_expression_arm*s, if such a type exists, and each *switch_expression_arm_expression* can be implicitly converted to that type.
3908
+
- It is an error if no such type exists.
3909
+
3910
+
It is an error if some *switch_expression_arm*'s pattern cannot affect the result because some previous pattern and guard will always match.
3911
+
3912
+
A switch expression is said to be *exhaustive* if every value of its input is handled by at least one arm of the switch expression. The compiler shall produce a warning if a switch expression is not exhaustive.
3913
+
At runtime, the result of the *switch_expression* is the value of the *expression* of the first *switch_expression_arm* for which the expression on the left-hand-side of the *switch_expression* matches the *switch_expression_arm*'s pattern, and for which the *case_guard* of the *switch_expression_arm*, if present, evaluates to `true`. If there is no such *switch_expression_arm*, the *switch_expression* throws an instance of the exception `System.Runtime.CompilerServices.SwitchExpressionException`.
3914
+
3915
+
> *Example*: The following converts values of an enum representing visual directions on an online map to the corresponding cardinal directions:
@@ -3883,10 +3952,10 @@ The `*`, `/`, `%`, `+`, and `-` operators are called the arithmetic operators.
3883
3952
3884
3953
```ANTLR
3885
3954
multiplicative_expression
3886
-
: range_expression
3887
-
| multiplicative_expression '*' range_expression
3888
-
| multiplicative_expression '/' range_expression
3889
-
| multiplicative_expression '%' range_expression
3955
+
:switch_expression
3956
+
|multiplicative_expression'*'switch_expression
3957
+
|multiplicative_expression'/'switch_expression
3958
+
|multiplicative_expression'%'switch_expression
3890
3959
;
3891
3960
3892
3961
additive_expression
@@ -4346,7 +4415,12 @@ equality_expression
4346
4415
;
4347
4416
```
4348
4417
4349
-
> *Note*: Lookup for the right operand of the `is` operator must first test as a *type*, then as an *expression* which may span multiple tokens. In the case where the operand is an *expression*, the pattern expression must have precedence at least as high as *shift_expression*. *end note*
4418
+
> *Note*: Lookup for the right operand of the `is` operator must first test as a *type*, then as an *expression* which may span multiple tokens. In the case where the operand is an *expression*, the pattern expression must have precedence at least as high as *shift_expression*. *end note*
4419
+
4420
+
<!-- markdownlint-disable MD028 -->
4421
+
4422
+
<!-- markdownlint-enable MD028 -->
4423
+
> *Note*: There is a grammar ambiguity between *type* and *constant_pattern* in a `relational_expression` on the right-hand-side of `is`; either might be a valid parse of a qualified identifier. In such a case, only if it fails to bind as a type (for compatibility with previous versions of the language), is it resolved to be the first thing found (which must be either a constant or a type). This ambiguity is only present on the right-hand side of such an expression.
4350
4424
4351
4425
The `is` operator is described in [§12.13.12](expressions.md#121312-the-is-operator) and the `as` operator is described in [§12.13.13](expressions.md#121313-the-as-operator).
4352
4426
@@ -4713,24 +4787,24 @@ There are two forms of the `is` operator. One is the *is-type operator*, which h
4713
4787
4714
4788
The *is-type operator* is used to check if the run-time type of an object is compatible with a given type. The check is performed at runtime. The result of the operation `E is T`, where `E` is an expression and `T` is a type other than `dynamic`, is a Boolean value indicating whether `E` is non-null and can successfully be converted to type `T` by a reference conversion, a boxing conversion, an unboxing conversion, a wrapping conversion, or an unwrapping conversion.
4715
4789
4716
-
The operation is evaluated as follows:
4790
+
The operation `E is T`is evaluated as follows:
4717
4791
4718
4792
1. If `E` is an anonymous function or method group, a compile-time error occurs.
4793
+
1. If `T` is a nullable reference type ([§8.9.3](types.md#893-nullable-reference-types)), a compile-time error occurs.
4719
4794
1. If `E` is the `null` literal, or if the value of `E` is `null`, the result is `false`.
4720
4795
1. Otherwise:
4721
-
1. Let `R` be the runtime type of `E`.
4722
-
1. Let `D` be derived from `R` as follows:
4723
-
1. If `R` is a nullable value type, `D` is the underlying type of `R`.
4724
-
1. Otherwise, `D` is `R`.
4725
-
1. The result depends on `D` and `T` as follows:
4726
-
1. If `T` is a reference type, the result is `true` if:
4727
-
- an identity conversion exists between `D` and `T`,
4728
-
-`D` is a reference type and an implicit reference conversion from `D` to `T` exists, or
4729
-
- Either: `D` is a value type and a boxing conversion from `D` to `T` exists.
4730
-
Or: `D` is a value type and `T` is an interface type implemented by `D`.
4731
-
1. If `T` is a nullable value type, the result is `true` if `D` is the underlying type of `T`.
4732
-
1. If `T` is a non-nullable value type, the result is `true` if `D` and `T` are the same type.
4733
-
1. Otherwise, the result is `false`.
4796
+
1. Let `R` be the runtime type of `E`.
4797
+
1. Let `D` be derived from `R` as follows:
4798
+
1. If `R` is a nullable value type, `D` is the underlying type of `R`.
4799
+
1. Otherwise, `D` is `R`.
4800
+
1. The result depends on `D` and `T` as follows:
4801
+
1. If `T` is a reference type, the result is `true` if:
4802
+
- an identity conversion exists between `D` and `T`, or
4803
+
-`D` is a reference type and an implicit reference conversion from `D` to `T` exists, or
4804
+
-`D` is a value type and a boxing conversion from `D` to `T` exists.
4805
+
1. If `T` is a nullable value type, the result is `true` if `D` is the underlying type of `T`.
4806
+
1. If `T` is a non-nullable value type, the result is `true` if `D` and `T` are the same type.
4807
+
1. Otherwise, the result is `false`.
4734
4808
4735
4809
User defined conversions are not considered by the `is` operator.
4736
4810
@@ -4758,6 +4832,8 @@ For an expression of the form `E is P`, where `E` is a relational expression of
4758
4832
-`E` does not designate a value or does not have a type.
4759
4833
- The pattern `P` is not applicable ([§11.2](patterns.md#112-pattern-forms)) to the type `T`.
4760
4834
4835
+
Every *single_variable_designation* of the pattern introduces a new local variable that is *definitely assigned* ([§9.4](variables.md#94-definite-assignment)) when the corresponding *relational_expression* tests `true`.
4836
+
4761
4837
### 12.13.13 The as operator
4762
4838
4763
4839
The `as` operator is used to explicitly convert a value to a given reference type or nullable value type. Unlike a cast expression ([§12.9.8](expressions.md#1298-cast-expressions)), the `as` operator never throws an exception. Instead, if the indicated conversion is not possible, the resulting value is `null`.
0 commit comments