Appendix E Operators
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.
Arithmetic Operators
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 |
Comparison Operators
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 . |
Logical Operators
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 |
Get C# 5.0 Programmer's Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.