关于C# 8中的Hat运算符(^)
这个运算符的新用法是自动创建 Index
结构体的实例。那什么是 Index
结构呢?这在 C# 8 中也有介绍。
Index
结构体的代码(就像所有的 C# 代码一样)可以在 github 上找到。你可以看到,它是一个相当简单的结构体,包含一个整数值,和一个定义是否应该从末尾开始计数的布尔值。
到目前为止,当尝试访问数组中特定索引处的值时,我们总是从第一个元素开始考虑。那么 Hat 运算符(^
)是如何帮助我们的呢?例如,如果你想获取一个已知大小的数组的最后一个的元素,你通常会从数组的 Length 中减去 1,并在检索时使用这个技巧或硬编码的数字。
例如:
int[] array = new int[] { 1, 3, 5, 7, 9 };
var x = array[4];
你可以像下面的例子一样使用数组提供的变量:
int[] array = new int[] { 1, 3, 5, 7, 9 };
var x = array[array.Length - 1];
我们可以改写成
适当的使用 Index
结构,我们可以很容易地创建一个索引值类型,这样我们就可以存储它并随心地重用它,以避免违反 DRY 原则。
Index lastItem = new Index(4, false);
int[] array = new int[] { 1, 3, 5, 7, 9 };
var x = array[lastItem];
但正如我们所看到的,我们还可以使用 fromEnd 参数来更好地表达我们希望检索最后一项,并在编译时从一个大小未知的数组中检索最后一项。
Index lastItem = new Index(1, true);
int[] array = new int[] { 1, 3, 5, 7, 9 };
var x = array[lastItem];
不过,我们需要记住的一点是,当从末尾开始计数时,不是以 0 开始的索引。把它想象成我们使用的 Length - x
,其中 x
就是我们在 Index
结构体构造函数中使用的值。
但是 Hat 运算符(^
)在这一切中有何用武之地呢?唔,Hat 运算符(^
) 是调用 Index
结构体时将 fromEnd 设置为 true 的简写方式。比如,下面的两行是完全相同的:
Index lastItem = new Index(1, true);// line 1
int[] array = new int[] { 1, 3, 5, 7, 9 };
var x = array[lastItem];
Index lastItem = ^1;//line 2, 同 line 1
int[] array = new int[] { 1, 3, 5, 7, 9 };
var x = array[lastItem];
或者,如果你想要检索倒数第二项,你可以这么做:
Index secondToLast = new Index(2, true);
int[] array = new int[] { 1, 3, 5, 7, 9 };
var x = array[secondToLast];
Index secondToLast = ^2;
int[] array = new int[] { 1, 3, 5, 7, 9 };
var x = array[secondToLast];
就这样,一个新的结构体类型和一个旧运算符(^
)的新用法包装在一起了。