Evenimente
17 mar., 21 - 21 mar., 10
Alăturați-vă seriei de întâlniri pentru a construi soluții AI scalabile bazate pe cazuri de utilizare din lumea reală cu colegi dezvoltatori și experți.
Înregistrați-vă acumAcest browser nu mai este acceptat.
Faceți upgrade la Microsoft Edge pentru a profita de cele mai noi funcții, actualizări de securitate și asistență tehnică.
The compiler generates the following errors for invalid pattern match expressions:
The compiler generates the following warnings for incomplete pattern matching expressions:
The compiler generates CS9134 to help you convert a switch statement to a switch expression. The case
keyword isn't used in a switch expression. If you have the case
keyword in a switch expression, it must be removed:
var answer = x switch
{
// CS9134: remove the keyword "case" from each switch arm:
case 0 => false,
case 1 => true,
}
The compiler generates CS9135 when the pattern isn't a constant value. You can't create pattern matching expressions to match against a variable.
The following code snippets generate CS8509:
// CS8509.cs
enum EnumValues
{
Value1,
Value2,
Value3
}
void Method(EnumValues enumValues)
{
var result = enumValues switch
{
EnumValues.Value1 => 1,
EnumValues.Value2 => 2,
};
}
To address this warning, add switch arms that cover all possible input values. For example:
enum EnumValues
{
Value1,
Value2,
Value3
}
void Method(EnumValues enumValues)
{
var result = enumValues switch
{
EnumValues.Value1 => 1,
EnumValues.Value2 => 2,
EnumValues.Value3 => 3,
_ => throw new ArgumentException("Input isn't a valid enum value", nameof(enumValues)),
};
}
The _
pattern matches all remaining values. One scenario for the _
pattern is matching invalid values, as shown in the preceding example.
For more information, see Switch.
Feedback pentru .NET
.NET este un proiect open source. Selectați un link pentru a oferi feedback:
Evenimente
17 mar., 21 - 21 mar., 10
Alăturați-vă seriei de întâlniri pentru a construi soluții AI scalabile bazate pe cazuri de utilizare din lumea reală cu colegi dezvoltatori și experți.
Înregistrați-vă acum