使用范围运算符 (IDE0057)
财产 | 价值 |
---|---|
规则 ID | IDE0057 |
标题 | 使用范围运算符 |
类别 | 样式 |
子类别 | 语言规则(表达式级首选项) |
适用的语言 | C# 8.0+ |
选项 | csharp_style_prefer_range_operator |
概述
此样式规则涉及使用 范围运算符 (..
),该运算符在 C# 8.0 及更高版本中可用。
选项
选项指定希望规则强制实施的行为。 有关配置选项的信息,请参阅 选项格式。
csharp_style_prefer_range_operator
财产 | 价值 | 描述 |
---|---|---|
选项名称 | csharp_style_prefer_range_operator | |
选项值 | true |
在提取集合的“切片”时,首选使用 range 运算符 .. |
false |
在提取集合的“切片”时,首选不使用范围运算符 .. |
|
默认选项值 | true |
// csharp_style_prefer_range_operator = true
string sentence = "the quick brown fox";
var sub = sentence[0..^4];
// csharp_style_prefer_range_operator = false
string sentence = "the quick brown fox";
var sub = sentence.Substring(0, sentence.Length - 4);
禁止显示警告
如果只想取消单个冲突,请将预处理器指令添加到源文件以禁用,然后重新启用规则。
#pragma warning disable IDE0057
// The code that's violating the rule is on this line.
#pragma warning restore IDE0057
若要禁用文件、文件夹或项目的规则,请将其严重性设置为 配置文件中的 none
。
[*.{cs,vb}]
dotnet_diagnostic.IDE0057.severity = none
若要禁用所有代码样式规则,请将类别 Style
的严重性设置为 配置文件中的 none
。
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
有关详细信息,请参阅 如何取消代码分析警告。