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# functions as both a unary operator for numeric negation and a binary operator for arithmetic subtraction, enumeration subtraction, delegate removal, and pointer arithmetic. Its behavior, return type, and overflow mechanics are resolved at compile-time based on the arity of the operation, the static types of its operands, and the evaluation context.
Unary Negation
When applied to a single operand, the- operator computes the numeric negation of that operand, effectively subtracting the operand from zero.
- Supported Types: Predefined for
int,long,float,double, anddecimal. - Type Mechanics: When the unary
-operator is applied to auint, the operand is implicitly promoted to along, and the operation returns along. The operator is not defined forulong; attempting to apply it to aulongresults in a compile-time error (CS0023). - Overflow Mechanics: The two’s complement representation of integers means the absolute value of
int.MinValueandlong.MinValueexceeds the maximum positive value of their respective types. Applying the unary-to these minimum values in anuncheckedcontext results in a wrap-around, returning the minimum value itself. In acheckedcontext, this operation throws aSystem.OverflowException.
Binary Subtraction
When applied to two operands, the- operator computes the arithmetic difference between the left operand (minuend) and the right operand (subtrahend).
- Supported Types: Predefined for all standard numeric types (
int,uint,long,ulong,float,double,decimal). - Type Mechanics: If the operands are of different types, C# applies implicit numeric promotions to convert them to a common type before performing the subtraction. The return type matches this promoted type.
- Overflow Mechanics: For integral types, if the mathematical difference cannot be represented within the bounds of the return type, an arithmetic overflow occurs. In an
uncheckedcontext, the high-order bits are discarded, resulting in a wrap-around. In acheckedcontext, the operation throws aSystem.OverflowException. Floating-point subtraction (float,double) never throws an overflow exception; instead, it yields positive or negative infinity.
Lifted Operators for Nullable Types
For nullable value types (T?), C# automatically provides lifted versions of the predefined unary and binary - operators. The lifted operator evaluates the HasValue property of its operands. If either or both operands evaluate to null, the result of the operation is null. Otherwise, the operator unwraps the underlying values, applies the standard - operator, and wraps the result back into a nullable type.
DateTime and TimeSpan Subtraction
C# provides predefined binary- operators for the System.DateTime, System.DateTimeOffset, and System.TimeSpan structs to handle chronological arithmetic:
- DateTime and DateTime: Subtracting a
DateTimefrom anotherDateTimeyields aSystem.TimeSpanrepresenting the duration between the two points in time. - DateTime and TimeSpan: Subtracting a
TimeSpanfrom aDateTimeyields a newDateTimedecremented by the specified duration. - TimeSpan and TimeSpan: Subtracting a
TimeSpanfrom anotherTimeSpanyields a newTimeSpanrepresenting the difference between the two durations.
Enumeration Subtraction
C# provides predefined binary- operators for all enum types, supporting two distinct operations:
- Enum and Enum: Subtracting two enum values of the same type yields the difference between their underlying numeric values. The return type is the underlying integral type of the enumeration.
- Enum and Integer: Subtracting an integer from an enum value yields a new enum value decremented by the integer amount. The return type is the enumeration type itself.
Delegate Removal
For operands of concrete delegate types (such asSystem.Action, System.Func<T>, or custom delegate signatures), the binary - operator performs delegate removal. It evaluates the invocation lists of both operands. The operator is not defined on the System.Delegate base class itself; attempting to use it on variables typed strictly as System.Delegate results in a compile-time error (CS0019).
- Mechanics: If the right operand’s invocation list is a contiguous sublist of the left operand’s invocation list, the operator returns a new delegate with that sublist removed. If the right operand’s invocation list matches multiple sublists within the left operand’s invocation list, the
-operator removes only the last matching contiguous sublist. If the right operand is not found, or if the right operand isnull, it returns the left operand unchanged. If the removal results in an empty invocation list, the operator returnsnull.
Pointer Arithmetic
In anunsafe context, the binary - operator supports pointer arithmetic with two distinct signatures:
- Pointer and Integer: Subtracting an integer offset from a pointer returns a new pointer of the same type, decremented by the offset multiplied by the size of the pointer’s underlying type.
- Pointer and Pointer: Subtracting a pointer from another pointer of the same type returns a
longrepresenting the distance (in elements, not bytes) between the two memory addresses.
Operator Overloading
User-defined types (struct or class) can overload the - operator to define custom negation or subtraction logic. Overloads must be declared as public static. If a type overloads the binary - operator, it does not automatically overload the -= compound assignment operator; however, C# automatically evaluates x -= y as x = x - y using the overloaded binary operator.
Master C# with Deep Grasping Methodology!Learn More





