使用复合赋值(IDE0054和IDE0074)

本文介绍两个相关规则,IDE0054IDE0074

财产 价值
规则 ID IDE0054
标题 使用复合分配
类别 样式
子类别 语言规则(表达式级首选项)
适用的语言 C# 和 Visual Basic
选项 dotnet_style_prefer_compound_assignment
财产 价值
规则 ID IDE0074
标题 使用联合复合赋值
类别 样式
子类别 语言规则(表达式级首选项)
适用的语言 C# 和 Visual Basic
选项 dotnet_style_prefer_compound_assignment

概述

这些规则涉及使用复合赋值。 联合复合赋值报告 IDE0074,其他复合赋值报告 IDE0054

选项

选项值指定是否需要复合赋值。

有关配置选项的信息,请参阅 选项格式

dotnet_style_prefer_compound_assignment

财产 价值 描述
选项名称 dotnet_style_prefer_compound_assignment
选项值 true 首选复合赋值表达式
false 不喜欢复合赋值表达式
默认选项值 true
// dotnet_style_prefer_compound_assignment = true
x += 5;

// dotnet_style_prefer_compound_assignment = false
x = x + 5;
' dotnet_style_prefer_compound_assignment = true
x += 5

' dotnet_style_prefer_compound_assignment = false
x = x + 5

禁止显示警告

如果只想取消单个冲突,请将预处理器指令添加到源文件以禁用,然后重新启用规则。

#pragma warning disable IDE0054 // Or IDE0074
// The code that's violating the rule is on this line.
#pragma warning restore IDE0054 // Or IDE0074

若要禁用文件、文件夹或项目的规则,请将其严重性设置为 配置文件中的 none

[*.{cs,vb}]
dotnet_diagnostic.IDE0054.severity = none
dotnet_diagnostic.IDE0074.severity = none

若要禁用所有代码样式规则,请将类别 Style 的严重性设置为 配置文件中的 none

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

有关详细信息,请参阅 如何取消代码分析警告

另请参阅