> ## 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.

# C# Unary Plus

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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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`.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
