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# is a heavily overloaded token that performs unary identity, binary arithmetic addition, string concatenation, delegate combination, enumeration addition, or pointer arithmetic, depending on the static types of its operands. While operator overload resolution and binding occur at compile-time, the actual evaluation of the operation executes at runtime, unless the operands are constant expressions.
Unary Plus Operator
The unary+ operator returns the value of its operand. It is predefined for all built-in numeric types.
+ operator performs numeric promotion. For example, applying it to a byte, short, or char implicitly converts the operand to an int.
Binary Arithmetic Addition
When applied to numeric types, the binary+ operator computes the sum of its two operands.
- Numeric Promotion: If the operands are of different types, the compiler applies implicit numeric conversions to align them to a common type before evaluation. Operations on integral types smaller than
int(likeshortorbyte) are promoted tointbefore the addition occurs, returning anint. - Integral Overflow Mechanics: For integral types, the behavior upon exceeding the type’s maximum value depends on the execution context. In an
uncheckedcontext (the default), the operation wraps around and emits theaddIntermediate Language (IL) instruction. In acheckedcontext, it emits theadd.ovfIL instruction and throws aSystem.OverflowException. - Floating-Point Mechanics: For
floatanddouble, addition is performed according to IEEE 754 arithmetic. Overflow results in positive or negative infinity rather than an exception. - Decimal Mechanics: For the
decimaltype, addition is performed with higher precision and a smaller range than floating-point types. Unlike integral and floating-point types,decimaladdition always throws aSystem.OverflowExceptionif the result exceedsdecimal.MaxValueordecimal.MinValue, regardless of whether the operation is in acheckedoruncheckedcontext.
String Concatenation
If at least one operand in a binary+ operation is of type string, the operator functions as a string concatenator.
- Underlying Implementation: The C# compiler translates the
+operator into a direct call to theSystem.String.Concat()method. If multiple+operators are chained, the compiler optimizes this into a singleString.Concatcall taking an array or multiple arguments to minimize intermediate string allocations. - Non-String Operands: For non-string reference types, the compiler emits a call to an overload like
System.String.Concat(object, object). TheString.Concatmethod handlesnullchecks internally and invokesToString()at runtime. This prevents aNullReferenceExceptionif the non-string operand is anullreference. Value types concatenated with strings using the+operator are boxed when passed toSystem.String.Concat(object, object).
Delegate Combination
When both operands are of the same delegate type, the+ operator combines them into a new multicast delegate.
- Underlying Implementation: The compiler translates this operation into a call to
System.Delegate.Combine(delegate1, delegate2). - Invocation List: The resulting multicast delegate contains an invocation list that executes the left operand’s target method(s) followed by the right operand’s target method(s) sequentially.
- Null Operands: If one operand is
null, the+operator simply returns the non-null delegate instance without creating a new multicast delegate. If both operands arenull, the operation returnsnull.
Enumeration Addition
The C# language specification explicitly defines built-in+ operator support for adding an integral type to an enumeration type.
- Underlying Mechanics: The addition is performed on the underlying integral type of the enumeration. The integer operand is added to the underlying value of the enum operand, and the result is cast back to the enumeration type.
Pointer Arithmetic
In anunsafe context, the + operator is natively supported for adding an integer offset to a pointer type.
- Memory Offset: The integer operand is multiplied by the size of the pointer’s target type (in bytes) before being added to the memory address. This operation is only valid for pointers to unmanaged types and cannot be applied to
void*pointers, as their size is unknown.
Operator Overloading
User-defined types (class or struct) can overload the + operator to define custom behavior.
- Constraints: The overload must be declared as
public static. For a binary+, at least one of the parameters must be of the containing type. For a unary+, the single parameter must be of the containing type. - Immutability: By convention and design, overloaded
+operators should not mutate either operand, but instead instantiate and return a new object representing the result.
Master C# with Deep Grasping Methodology!Learn More





