Uwaga
Dostęp do tej strony wymaga autoryzacji. Może spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
W tym artykule opisano dwie powiązane reguły, IDE0047
i IDE0048
.
Własność | Wartość |
---|---|
identyfikator reguły | IDE0047 |
Tytuł | Usuwanie niepotrzebnych nawiasów |
Kategoria | Styl |
podkategorii | Reguły języka (preferencje nawiasów) |
odpowiednie języki | C# i Visual Basic |
Wprowadzono wersję | Visual Studio 2017 |
opcje | dotnet_style_parentheses_in_arithmetic_binary_operators |
dotnet_style_parentheses_in_relational_binary_operators |
|
dotnet_style_parentheses_in_other_binary_operators |
|
dotnet_style_parentheses_in_other_operators |
Własność | Wartość |
---|---|
identyfikator reguły | IDE0048 |
Tytuł | Dodawanie nawiasów w celu uzyskania jasności |
Kategoria | Styl |
Podkategoria | Reguły języka (preferencje nawiasów) |
Stosowane języki | C# i Visual Basic |
Wprowadzenie wersji | Visual Studio 2017 |
Opcje | dotnet_style_parentheses_in_arithmetic_binary_operators |
dotnet_style_parentheses_in_relational_binary_operators |
|
dotnet_style_parentheses_in_other_binary_operators |
|
dotnet_style_parentheses_in_other_operators |
Przegląd
Reguły stylu w tej sekcji dotyczą preferencji nawiasów, w tym użycia nawiasów w celu wyjaśnienia pierwszeństwa dla arytmetycznych, relacyjnych i innych operatorów binarnych.
Opcje
Ta reguła ma skojarzone opcje określania preferencji na podstawie typu operatora:
- Operatory binarne arytmetyczne — dotnet_style_parentheses_in_arithmetic_binary_operators
- Operatory relacyjne binarne — dotnet_style_parentheses_in_relational_binary_operators
- Inne operatory binarne — dotnet_style_parentheses_in_other_binary_operators
- Inne operatory — dotnet_style_parentheses_in_other_operators
Aby uzyskać informacje na temat konfigurowania opcji, zobacz Format opcji.
dotnet_style_parentheses_in_arithmetic_binary_operators
Własność | Wartość | Opis |
---|---|---|
nazwa opcji | dotnet_style_parentheses_in_arithmetic_binary_operators | |
wartości opcji | always_for_clarity |
Preferuj nawiasy, aby wyjaśnić pierwszeństwo operatora arytmetycznego |
never_if_unnecessary |
Preferuj brak nawiasów, gdy pierwszeństwo operatora arytmetycznego jest oczywiste | |
domyślna wartość opcji | always_for_clarity |
Operatory binarne arytmetyczne to: *
, /
, %
, +
, -
, <<
, >>
, &
, ^
i |
.
// dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
var v = a + (b * c);
// dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary
var v = a + b * c;
' dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
Dim v = a + (b * c)
' dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary
Dim v = a + b * c
dotnet_style_parentheses_in_relational_binary_operators
Własność | Wartość | Opis |
---|---|---|
nazwa opcji | Styl_netowy_nawiasów_w_operatorach_relacyjnych_binarnych | |
wartości opcji | always_for_clarity |
Preferuj nawiasy, aby wyjaśnić pierwszeństwo operatora relacyjnego |
never_if_unnecessary |
Preferuj nie mieć nawiasów, gdy pierwszeństwo operatora relacyjnego jest oczywiste | |
domyślna wartość opcji | always_for_clarity |
Operatory relacyjne binarne to: >
, <
, <=
, >=
, is
, as
, ==
i !=
.
// dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity
var v = (a < b) == (c > d);
// dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary
var v = a < b == c > d;
' dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity
Dim v = (a < b) = (c > d)
' dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary
Dim v = a < b = c > d
dotnet_style_parentheses_in_other_binary_operators
Własność | Wartość | Opis |
---|---|---|
nazwa opcji | dotnet_style_parentheses_in_other_binary_operators | |
Wartości opcji | always_for_clarity |
Preferuj nawiasy, aby wyjaśnić inne pierwszeństwo operatora binarnego |
never_if_unnecessary |
Preferuj nie mieć nawiasów, gdy inne pierwszeństwo operatora binarnego jest oczywiste | |
domyślna wartość opcji | always_for_clarity |
Inne operatory binarne to: &&
, ||
i ??
.
// dotnet_style_parentheses_in_other_binary_operators = always_for_clarity
var v = a || (b && c);
// dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary
var v = a || b && c;
' dotnet_style_parentheses_in_other_binary_operators = always_for_clarity
Dim v = a OrElse (b AndAlso c)
' dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary
Dim v = a OrElse b AndAlso c
dotnet_style_parentheses_in_other_operators
Własność | Wartość | Opis |
---|---|---|
nazwa opcji | dotnet_style_parentheses_in_other_operators | |
Wartości Opci | always_for_clarity |
Preferuj nawiasy, aby wyjaśnić inne pierwszeństwo operatora |
never_if_unnecessary |
Preferuj nie mieć nawiasów, gdy pierwszeństwo innych operatorów jest oczywiste | |
domyślna wartość opcji | never_if_unnecessary |
Ta opcja dotyczy operatorów innych niż następujących:
*
, /
, %
, +
, -
, <<
, >>
, &
, ^
, |
>
, <
, <=
, >=
, is
, as
, ==
, !=
&&
||
, ??
// dotnet_style_parentheses_in_other_operators = always_for_clarity
var v = (a.b).Length;
// dotnet_style_parentheses_in_other_operators = never_if_unnecessary
var v = a.b.Length;
' dotnet_style_parentheses_in_other_operators = always_for_clarity
Dim v = (a.b).Length
' dotnet_style_parentheses_in_other_operators = never_if_unnecessary
Dim v = a.b.Length
Pomijanie ostrzeżenia
Jeśli chcesz pominąć tylko jedno naruszenie, dodaj dyrektywy preprocesora do pliku źródłowego, aby wyłączyć, a następnie ponownie włączyć regułę.
#pragma warning disable IDE0047 // Or IDE0048
// The code that's violating the rule is on this line.
#pragma warning restore IDE0047 // Or IDE0048
Aby wyłączyć regułę dla pliku, folderu lub projektu, ustaw jego ważność na none
w pliku konfiguracji .
[*.{cs,vb}]
dotnet_diagnostic.IDE0047.severity = none
dotnet_diagnostic.IDE0048.severity = none
Aby wyłączyć wszystkie reguły stylu kodu, ustaw poziom ważności dla kategorii Style
na none
w pliku konfiguracyjnym .
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
Aby uzyskać więcej informacji, zobacz Jak pominąć ostrzeżenia analizy kodu.