A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/subtraction-operator below:

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

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:

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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4