The C# operators fall into four main categories: arithmetic, comparison, logical, and bitwise. The following sections explain these categories and the operators they contain. The end of this appendix describes operator precedence and special operators, as well as operator overloading.
The following table lists the arithmetic operators provided by C#.
Operator | Purpose | Example | Result |
++ | Increment | x++ or ++x | Sets x = x + 1 |
-- | Decrement | x-- or --x | Sets x = x - 1 |
– | Negation | -x | Sets x = -x |
+ | Unary plus | +x | Sets x = +x (leaves x unchanged) |
* | Multiplication | 2 * 3 | 6 |
/ | Division | 3.0 / 2 | 1.5 |
% | Modulus | 17 % 5 | 2 |
+ | Addition | 2 + 3 | 5 |
+ | Concatenation | "Bob " + "Baker" | "Bob Baker" |
- | Subtraction | 3 - 2 | 1 |
<< | Bit left shift | 10110111 << 1 | 01101110 |
>> | Bit right shift | 10110111 >> 1 | 01011011 |
The following table lists the comparison operators provided by C#.
Operator | Purpose | Example | Result |
== | Equals | A == B | true if A equals B . |
!= | Not equals | A != B | true if A does not equal B . |
< | Less than | A < B | true if A is less than B . |
<= | Less than or equal to | A <= B | true if A is less than or equal to B . |
> | Greater than | A > B | true if A is greater than B . |
>= | Greater than or equal to | A >= B | true if A is greater than or equal to B . |
is | Object is or inherits from a certain type | obj is Manager | true if obj is an object that inherits from Manager . |
The following table summarizes the C# logical operators.
Operator | Purpose | Example | Result |
! | Negation | !A | true if A is false . |
& | And | A & B | true if A and B are both true . |
| | Or | A | B | true if A or B or both are true . |
^ | Xor (Exclusive Or) | A ^ B | true if A is true or B is true but both are not true . |
&& | And with short-circuit evaluation | A && B | true if A and B are both true . |
|| | Or with short-circuit evaluation | A || B | true if A or B or both are true . |
Bitwise operators work much as logical operators do, except that they compare values 1 bit at a time. C# provides bitwise versions of the &
, |
, and ^
operators. It also provides ~
, the bitwise negation operator.
Many operators have assignment versions. For example, the following code adds 10 to the value x
and saves the result in variable x
.
x += 10;
The complete list of assignment operators is: =
, +=
, -=
, *=
, /=
, %=
, &=
, |=
, ^=
, <<=
, and >>=
.
The conditional operator ?:
(sometimes called the ternary operator) takes three operands. If the first operand is true
, it returns the second operand. Otherwise it returns the third operand.
The null-coalescing operator ??
takes two operands. It returns its left operand if its value is not null
. If the left operand is null
, it returns its right operand.
The following table lists the operators in order of precedence. When evaluating an expression, the program evaluates an operator before it evaluates those in a lower section of the list.
Operator | Description |
( ) | Grouping (parentheses) |
x++ x-- | Post-increment Post-decrement |
+ - ! ~ ++x --x (T) | Unary plus Numeric negation Logical negation Bitwise negation Pre-increment Pre-decrement Casting |
* / % | Multiplication Division Modulus |
+ + - | Concatenation Addition Subtraction |
<< >> | Left shift Right shift |
< > <= >= is | Less than Greater than Less than or equal to Greater than or equal to Inherits from |
== != | Equals Does not equal |
& | Logical And |
^ | Logical Xor |
| | Logical Or |
&& | Conditional And |
|| | Conditional Or |
?? | Null-coalescing |
?: | Conditional |
= += -= ... | Assignment operators |
When operators are in the same section in the table, or if an expression contains more than one instance of the same operator, the program evaluates them in left-to-right order.
Use parentheses to change the order of evaluation and to make expressions easier to read.
The DateTime
and TimeSpan
data types are related through their operators. The following list shows the relationships between these two data types.
DateTime − DateTime = TimeSpan
DateTime + TimeSpan = DateTime
TimeSpan + TimeSpan = TimeSpan
TimeSpan − TimeSpan = TimeSpan
The following table lists examples demonstrating convenient methods provided by the DateTime
data type.
Syntax | Meaning |
newDate = date1.Add(timespan1) | Returns date1 plus timespan1 |
newDate = date1.AddYears(numYears) | Returns date1 plus the indicated number of years |
newDate = date1.AddMonths(numMonths) | Returns date1 plus the indicated number of months |
newDate = date1.AddDays(numDays) | Returns date1 plus the indicated number of days |
newDate = date1.AddHours(numHours) | Returns date1 plus the indicated number of hours |
newDate = date1.AddMinutes(numMinutes) | Returns date1 plus the indicated number of minutes |
newDate = date1.AddSeconds(numSeconds) | Returns date1 plus the indicated number of seconds |
newDate = date1.AddMilliseconds(numMilliseconds) | Returns date1 plus the indicated number of milliseconds |
newDate = date1.AddTicks(numTicks) | Returns date1 plus the indicated number of ticks (100 nanosecond units) |
newTimespan = date1.Subtract(date2) | Returns the time span between date2 and date1 |
resultInt = date1.CompareTo(date2) | Returns a value indicating whether date1 is greater than, less than, or equal to date2 |
resultBool = date1.Equals(date2) | Returns true if date1 equals date2 |
To overload an operator, create a static
method that returns the appropriate data type. Instead of giving the method a name, use the keyword operator
followed by the operator symbol you want to overload. Next, define the parameters that the operator takes. Finally, write the code that the operator should execute.
For example, the following code defines a +
operator for a simple Complex
class.
public static Complex operator +(Complex operand1, Complex operand2)
{
return new Complex()
{
Re = operand1.Re + operand2.Re,
Im = operand1.Im + operand2.Im
};
}
Unary operators that you can overload include: +
, -
, !
, ~
, ++
, --
, true
, and false
.
Binary operators that you can overload include: +
, -
, *
, /
, %
, &
, |
, ^
, <<
, and >>
. If you overload true
, false 7
, and |
, the &&
and ||
operators are automatically overloaded for you.
Note that the second operand for the shift operators << and >> must be an int
.
The assignment operators are automatically overloaded if you overload the corresponding operator. For example, if you overload *
, then C# overloads *=
for you.
See Chapter 5, “Operators,” for more information about overloading comparison, logical, and type conversion operators.
18.189.188.121