CKTIS / C#
Unary plus (+x)
The +x operator returns its operand's numeric value without changing its sign. It neither increments a variable nor computes an absolute value. For small integral types, the result has type int.
Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification
Syntax
+xThe +x operator takes one operand. For built-in numeric types, it returns that operand's value without changing its sign: if x is -7, then +x is also -7. This is neither an absolute value nor negation -x.
Do not confuse +x with addition x + y or increment ++x. Built-in unary plus does not assign anything to x or increase it by one.
Preserving the value does not always mean preserving the type. Operands of type sbyte, byte, short, ushort, and char are promoted to int, so var promoted = +small with a byte variable named small produces an int result.
User-defined types can overload unary +; their behavior is determined by the selected implementation. The example uses only built-in operators.
Examples
using System;
class Program
{
static void Main()
{
int x = -7;
Console.WriteLine(+x);
Console.WriteLine(x);
byte small = 12;
var promoted = +small;
Console.WriteLine(promoted);
Console.WriteLine(promoted.GetType().FullName);
}
}Result
-7 -7 12 System.Int32
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.