Skip to main content
The ^ 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 to true if and only if exactly one operand is true. If both operands share the same boolean value, it evaluates to false.
  • Integral Operands (int, uint, long, etc.): Performs a bitwise XOR. It compares the binary representations of the operands bit by bit, resulting in 1 where the corresponding bits differ, and 0 where they match.
The binary ^ 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.
Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More