O'Reilly logo

C# 5.0 Programmer's Reference by Rod Stephens

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required