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 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.
+ 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.
- 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
shortand anintresults in anint. - Overflow Context: For integer types, the behavior of the
+operator upon exceeding the type’s maximum value depends on the execution context. In anuncheckedcontext (the default), the result is truncated by discarding high-order bits. In acheckedcontext, aSystem.OverflowExceptionis thrown. - Floating-Point: For
floatanddouble, addition is performed according to IEEE 754 arithmetic, which includes handling forNaN(Not a Number) andInfinity.
Enumeration Addition
The+ operator is predefined to allow the addition of an enumeration type and its underlying integral type.
- 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 anunsafe context, the + operator allows adding an integer offset to a pointer.
- The pointer type
T*must be an unmanaged type, and the offset must be of typeint,uint,long, orulong. - Adding an integer
nto a pointerPof typeT*yields a new pointer positionedn * sizeof(T)bytes afterP.
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.
- The compiler translates the
+operation into a call toSystem.String.Concat(). - Modern C# compilers (Roslyn) optimize concatenation with value types by directly calling
.ToString()on the value type and emitting a call toString.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 aNullReferenceException. - Type Restrictions: Pointer types (
T*) andref structtypes (such asSpan<T>) cannot be implicitly converted toobjectorstring. 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.
- 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 arenull, it returnsnull.
Operator Overloading
Customclass and struct types can redefine the behavior of the + operator by declaring a public static method using the operator keyword.
- 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





