- and -= operators - subtraction (minus) operators - C# reference (original) (raw)

The built-in integral and floating-point numeric types and delegate types all support the - and -= operators.

For information about the arithmetic - operator, see the Unary plus and minus operators and Subtraction operator - sections of the Arithmetic operators article.

Delegate removal

For operands of the same delegate type, the - operator returns a delegate instance that is calculated as follows:

Action a = () => Console.Write("a");  
Action b = () => Console.Write("b");  
var abbaab = a + b + b + a + a + b;  
abbaab();  // output: abbaab  
Console.WriteLine();  
var ab = a + b;  
var abba = abbaab - ab;  
abba();  // output: abba  
Console.WriteLine();  
var nihil = abbaab - abbaab;  
Console.WriteLine(nihil is null);  // output: True  
Action a = () => Console.Write("a");  
Action b = () => Console.Write("b");  
var abbaab = a + b + b + a + a + b;  
var aba = a + b + a;  
var first = abbaab - aba;  
first();  // output: abbaab  
Console.WriteLine();  
Console.WriteLine(object.ReferenceEquals(abbaab, first));  // output: True  
Action a2 = () => Console.Write("a");  
var changed = aba - a;  
changed();  // output: ab  
Console.WriteLine();  
var unchanged = aba - a2;  
unchanged();  // output: aba  
Console.WriteLine();  
Console.WriteLine(object.ReferenceEquals(aba, unchanged));  // output: True  

The preceding example also demonstrates that during delegate removal delegate instances are compared. For example, delegates that are produced from evaluation of identical lambda expressions aren't equal. For more information about delegate equality, see the Delegate equality operators section of the C# language specification.

Action a = () => Console.Write("a");  
var nothing = null - a;  
Console.WriteLine(nothing is null);  // output: True  
var first = a - null;  
a();  // output: a  
Console.WriteLine();  
Console.WriteLine(object.ReferenceEquals(first, a));  // output: True  

To combine delegates, use the + operator.

For more information about delegate types, see Delegates.

Subtraction assignment operator -=

An expression using the -= operator, such as

x -= y

Is equivalent to

x = x - y

Except that x is only evaluated once.

The following example demonstrates the usage of the -= operator:

int i = 5;
i -= 9;
Console.WriteLine(i);
// Output: -4

Action a = () => Console.Write("a");
Action b = () => Console.Write("b");
var printer = a + b + a;
printer();  // output: aba

Console.WriteLine();
printer -= a;
printer();  // output: ab

You also use the -= operator to specify an event handler method to remove when you unsubscribe from an event. For more information, see How to subscribe to and unsubscribe from events.

Operator overloadability

A user-defined type can overload the - operator. When a binary - operator is overloaded, the -= operator is also implicitly overloaded. Beginning with C# 14, a user-defined type can explicitly overload the -= operator to provide a more efficient implementation. Typically, a type overloads the -= operator because the value can be updated in place, rather than allocating a new instance to store the result of the subtraction. If a type doesn't provide an explicit overload, the compiler generates the implicit overload.

C# language specification

For more information, see the Unary minus operator and Subtraction operator sections of the C# language specification. For more information on overloading the compound assignment operators in C# 14 and later, see the user defined compound assignment feature specification.

See also