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#.

OperatorPurposeExampleResult
++Incrementx++ or ++xSets x = x + 1
--Decrementx-- or --xSets x = x - 1
Negation-xSets x = -x
+Unary plus+xSets x = +x (leaves x unchanged)
*Multiplication2 * 36
/Division3.0 / 21.5
%Modulus17 % 52
+Addition2 + 35
+Concatenation"Bob " + "Baker""Bob Baker"
-Subtraction3 - 21
<<Bit left shift10110111 << 101101110
>>Bit right shift10110111 >> 101011011

Comparison Operators

The following table lists the comparison operators provided by C#.

OperatorPurposeExampleResult
==EqualsA == Btrue if A equals B.
!=Not equalsA != Btrue if A does not equal B.
<Less thanA < Btrue if A is less than B.
<=Less than or equal toA <= Btrue if A is less than or equal to B.
>Greater thanA > Btrue if A is greater than B.
>=Greater than or equal toA >= Btrue if A is greater than or equal to B.
isObject is or inherits from a certain typeobj is Managertrue if obj is an object that inherits from Manager.

Logical Operators

The following table summarizes the C# logical operators.

OperatorPurposeExampleResult
!Negation!Atrue if A is false.
&AndA & Btrue 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.