TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
^ operator in C# serves two distinct syntactic roles depending on its context: as a binary Logical/Bitwise Exclusive OR (XOR) operator, and as a unary Index from End operator.
1. Logical and Bitwise Exclusive OR (XOR) Operator
When used as a binary operator between two operands,^ computes the exclusive OR. The evaluation mechanics depend strictly on the operand types:
- Boolean Operands (
bool): Performs a logical XOR. It evaluates totrueif and only if exactly one operand istrue. If both operands share the same boolean value, it evaluates tofalse. - Integral Operands (
int,uint,long, etc.): Performs a bitwise XOR. It compares the binary representations of the operands bit by bit, resulting in1where the corresponding bits differ, and0where they match.
^ operator also supports compound assignment via ^=, which applies the XOR operation and assigns the result to the left operand.
2. Index from End Operator
Introduced in C# 8.0, when used as a unary prefix operator,^ instantiates a System.Index struct. It specifies an index relative to the end of a sequence (such as an array, Span<T>, or string), rather than the beginning.
The index ^n translates to sequence.Length - n at runtime. Consequently, ^1 points to the last element in the sequence. ^0 points to the sequence length itself; while ^0 throws an IndexOutOfRangeException if used for direct element access, it is syntactically valid and required when defining an exclusive upper bound in a System.Range.
Master C# with Deep Grasping Methodology!Learn More





