C# - 一元 ^ 的作用是什么?
C# - what does the unary ^ do?
我检查了一些代码,但在第
行中出现错误(准确地说是 'invalid expression term "^"')
// choices is a regular array
return choices[^1];
我从未见过一元插入符号运算符(我只知道 XOR 运算符,但它显然需要两个操作数)。该运算符是否存在,如果存在,它的作用是什么?
注意:站点 https://www.tutorialsteacher.com/csharp/csharp-operators 在优先级中提到了一个一元插入符运算符 table 但它没有解释它的作用。
一元 ^
是 "index from end" 运算符,在 C# 8.0 中引入。 choices[^1]
等同于 choices[choices.Length - 1]
.
有关更多详细信息,请参阅 the official documentation。
在数组中,它被称为“结束运算符的索引”,从 C# 8.0 开始可用。正如它所说...它表示从序列末尾开始的元素位置。
在第二种情况下,^
扮演逻辑异或运算符的角色。作为二元运算符,它需要两个成员 (x ^ y
),因为它正在对它们进行运算。
例如:
Console.WriteLine(true ^ true); // output: False
Console.WriteLine(true ^ false); // output: True
Console.WriteLine(false ^ true); // output: True
Console.WriteLine(false ^ false); // output: False
我检查了一些代码,但在第
行中出现错误(准确地说是 'invalid expression term "^"')// choices is a regular array
return choices[^1];
我从未见过一元插入符号运算符(我只知道 XOR 运算符,但它显然需要两个操作数)。该运算符是否存在,如果存在,它的作用是什么?
注意:站点 https://www.tutorialsteacher.com/csharp/csharp-operators 在优先级中提到了一个一元插入符运算符 table 但它没有解释它的作用。
一元 ^
是 "index from end" 运算符,在 C# 8.0 中引入。 choices[^1]
等同于 choices[choices.Length - 1]
.
有关更多详细信息,请参阅 the official documentation。
在数组中,它被称为“结束运算符的索引”,从 C# 8.0 开始可用。正如它所说...它表示从序列末尾开始的元素位置。
在第二种情况下,^
扮演逻辑异或运算符的角色。作为二元运算符,它需要两个成员 (x ^ y
),因为它正在对它们进行运算。
例如:
Console.WriteLine(true ^ true); // output: False
Console.WriteLine(true ^ false); // output: True
Console.WriteLine(false ^ true); // output: True
Console.WriteLine(false ^ false); // output: False