使用索引运算符 (IDE0056)

财产 价值
规则 ID IDE0056
标题 使用索引运算符
类别 样式
子类别 语言规则(表达式级首选项)
适用的语言 C# 8.0+
选项 csharp_style_prefer_index_operator

概述

此样式规则与是否使用 C# 8.0 及更高版本中提供的 index-from-end 运算符 (^)有关。

选项

选项指定希望规则强制实施的行为。 有关配置选项的信息,请参阅 选项格式

csharp_style_prefer_index_operator

财产 价值 描述
选项名称 csharp_style_prefer_index_operator
选项值 true 在从集合末尾计算索引时,首选使用 ^ 运算符
false 在从集合末尾计算索引时,不希望使用 ^ 运算符
默认选项值 true
// csharp_style_prefer_index_operator = true
string[] names = { "Archimedes", "Pythagoras", "Euclid" };
var index = names[^1];

// csharp_style_prefer_index_operator = false
string[] names = { "Archimedes", "Pythagoras", "Euclid" };
var index = names[names.Length - 1];

禁止显示警告

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

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

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

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

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

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

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

另请参阅