Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The + operator in C# is a heavily overloaded token that functions as a unary identity operator, a binary arithmetic addition operator, a string concatenation operator, a delegate combination operator, an enumeration offset operator, and a pointer arithmetic operator, depending on the compile-time types of its operands.

Unary Plus Operator

The unary + operator is predefined for all numeric types. It acts as an identity operator, returning the exact value of its operand.
int operand = 5;
int result = +operand; // result is 5
For user-defined types, the unary + can be overloaded. If an operand is of type byte, sbyte, short, or ushort, implicit numeric promotion occurs, and the operation returns an int.

Binary Arithmetic Addition

When both operands are of numeric types, the + operator computes their sum. It is predefined for integer (int, uint, long, ulong), floating-point (float, double), and decimal types.
int operand1 = 10;
int operand2 = 20;
int result = operand1 + operand2; // result is 30
Mechanics:
  • Numeric Promotion: If the operands are of different types, the compiler applies implicit numeric promotions to align them to a common type before evaluating the addition. For example, adding a short and an int results in an int.
  • Overflow Context: For integer types, the behavior of the + operator upon exceeding the type’s maximum value depends on the execution context. In an unchecked context (the default), the result is truncated by discarding high-order bits. In a checked context, a System.OverflowException is thrown.
  • Floating-Point: For float and double, addition is performed according to IEEE 754 arithmetic, which includes handling for NaN (Not a Number) and Infinity.

Enumeration Addition

The + operator is predefined to allow the addition of an enumeration type and its underlying integral type.
System.DayOfWeek day = System.DayOfWeek.Monday; // Underlying value is 1
System.DayOfWeek nextDay = day + 2; // result is System.DayOfWeek.Wednesday (3)
Mechanics:
  • The operation adds the integral value to the underlying numeric value of the enumeration.
  • The resulting numeric value is explicitly cast back to the enumeration type.

Pointer Arithmetic

In an unsafe context, the + operator allows adding an integer offset to a pointer.
unsafe 
{
    int[] numbers = { 10, 20, 30 };
    fixed (int* pointerOperand = &numbers[0])
    {
        int* result = pointerOperand + 1; // Points to numbers[1] (20)
    }
}
Mechanics:
  • The pointer type T* must be an unmanaged type, and the offset must be of type int, uint, long, or ulong.
  • Adding an integer n to a pointer P of type T* yields a new pointer positioned n * sizeof(T) bytes after P.

String Concatenation

If at least one operand in a binary + operation is of type string, the operator functions as a string concatenator. The non-string operand must be of a type that can be implicitly converted to object or string.
string stringOperand = "Value: ";
int intOperand = 42;
string result = stringOperand + intOperand; // result is "Value: 42"
Mechanics:
  • The compiler translates the + operation into a call to System.String.Concat().
  • Modern C# compilers (Roslyn) optimize concatenation with value types by directly calling .ToString() on the value type and emitting a call to String.Concat(string, string). This specifically avoids the performance penalty of boxing allocations that occurred in older .NET framework versions.
  • If an operand is null, it is treated as an empty string (String.Empty) rather than throwing a NullReferenceException.
  • Type Restrictions: Pointer types (T*) and ref struct types (such as Span<T>) cannot be implicitly converted to object or string. Attempting to use them as operands in a + string concatenation operation will result in a compilation error.

Delegate Combination

When both operands are of the same delegate type, the + operator combines them into a new multicast delegate.
System.Action delegate1 = () => System.Console.Write("A");
System.Action delegate2 = () => System.Console.Write("B");
System.Action combinedDelegate = delegate1 + delegate2; 
// Invoking combinedDelegate() outputs "AB"
Mechanics:
  • The compiler translates this operation into a call to System.Delegate.Combine(delegate1, delegate2).
  • The resulting multicast delegate contains the invocation lists of both operands in sequential order.
  • If either operand is null, the operator returns the non-null operand. If both are null, it returns null.

Operator Overloading

Custom class and struct types can redefine the behavior of the + operator by declaring a public static method using the operator keyword.
public struct Vector2
{
    public int X { get; }
    public int Y { get; }

    public Vector2(int x, int y)
    {
        X = x;
        Y = y;
    }

    // Binary addition overload
    public static Vector2 operator +(Vector2 a, Vector2 b)
    {
        return new Vector2(a.X + b.X, a.Y + b.Y);
    }

    // Unary identity overload
    public static Vector2 operator +(Vector2 a)
    {
        return new Vector2(+a.X, +a.Y);
    }
}
Mechanics:
  • At least one of the parameters in the overload signature must be of the containing type.
  • The method must return a value matching the specified return type.
  • Overloading the binary + operator implicitly overloads the += compound assignment operator. C# does not allow += to be overloaded directly.
Master C# with Deep Grasping Methodology!Learn More