Usar expressão switch (IDE0066)Use switch expression (IDE0066)
-
- 2 minutos para o fim da leitura
-
PropriedadeProperty |
ValorValue |
ID da regraRule ID |
IDE0066IDE0066 |
TítuloTitle |
Usar expressão switchUse switch expression |
CategoriaCategory |
EstiloStyle |
SubcategoriaSubcategory |
Regras de linguagem (preferências de correspondência de padrões)Language rules (pattern matching preferences) |
Idiomas aplicáveisApplicable languages |
C# 8.0+C# 8.0+ |
Versão introduzidaIntroduced version |
Visual Studio 2019 versão 16.2Visual Studio 2019 version 16.2 |
Visão geralOverview
Essa regra de estilo se refere ao uso de expressões switch versus instruções switch.This style rule concerns the use of switch expressions versus switch statements.
csharp_style_prefer_switch_expressioncsharp_style_prefer_switch_expression
PropriedadeProperty |
ValorValue |
Nome da opçãoOption name |
csharp_style_prefer_switch_expressioncsharp_style_prefer_switch_expression |
Valores de opçãoOption values |
true – Preferir usar uma expressão switch (introduzida com C# 8.0)true - Prefer to use a switch expression (introduced with C# 8.0)
false -Preferir usar uma switch instruçãofalse - Prefer to use a switch statement |
Valor da opção padrãoDefault option value |
true |
ExemploExample
// csharp_style_prefer_switch_expression = true
return x switch
{
1 => 1 * 1,
2 => 2 * 2,
_ => 0,
};
// csharp_style_prefer_switch_expression = false
switch (x)
{
case 1:
return 1 * 1;
case 2:
return 2 * 2;
default:
return 0;
}
Confira tambémSee also