Slide 8.9: Variables (cont.)
Slide 8.11: Operators (cont.)
Home

Operators


An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C# has a rich set of built-in operators and provides the following type of operators.

Arithmetic Operators
In C#, except for + and -, the symbols for the operators are different from normal mathematical operators, as shown below.

Operator Description Example
* Multiplication 4 * 3 = 12
/ Division 12 / 3 = 4
++ Increment (increases integer value by one) a = 10; a++ = 11
-- Decrement (decreases integer value by one) a = 10; a-- = 9
% Modulus (returns the remainder from an integer division) 15 % 4 = 3
+ String concatenation "C " + "Sharp" = "C Sharp"

Relational Operators
Following table shows all the relational operators supported by C#. Assume variable A holds 10 and variable B holds 20, then −

Operator Description Example
== Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
!= Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.