Επεξεργασία

Κοινή χρήση μέσω


- and -= operators - subtraction (minus)

The - and -= operators are supported by the built-in integral and floating-point numeric types and delegate types.

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:

  • If both operands are non-null and the invocation list of the right-hand operand is a proper contiguous sublist of the invocation list of the left-hand operand, the result of the operation is a new invocation list that is obtained by removing the right-hand operand's entries from the invocation list of the left-hand operand. If the right-hand operand's list matches multiple contiguous sublists in the left-hand operand's list, only the right-most matching sublist is removed. If removal results in an empty list, the result is null.

    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
    
  • If the invocation list of the right-hand operand isn't a proper contiguous sublist of the invocation list of the left-hand operand, the result of the operation is the left-hand operand. For example, removing a delegate that isn't part of the multicast delegate does nothing and results in the unchanged multicast delegate.

    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.

  • If the left-hand operand is null, the result of the operation is null. If the right-hand operand is null, the result of the operation is the left-hand operand.

    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. A user-defined type can't explicitly overload the -= operator.

C# language specification

For more information, see the Unary minus operator and Subtraction operator sections of the C# language specification.

See also