Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# Operators

C# Operators

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. C# has a rich set of built-in operators, categorized as follows:

This tutorial explains arithmetic, relational, logical, bitwise, assignment, and other operators one by one.

Arithmetic operators

The following table shows all arithmetic operators supported by C#. Assume variable A has the value 10, and variable B has the value 20, then:

operator Description Example
+ Adds the two operands A + B will give 30
- Subtracts the second operand from the first operand A - B will give -10
* Multiplies the two operands A * B will give 200
/ Divides numerator by denominator B / A will give 2
% Modulus operator, the remainder after integer division B % A will give 0
++ Increment operator, increases the integer value by 1 A++ will give 11
-- Decrement operator, decreases the integer value by 1 A-- will give 9

Example

Look at the following example to understand all available arithmetic operators in C#:

Example

using

System

;

namespace

OperatorsAppl

{

   

class

Program

   

{

       

static

void

Main

(

string

[

]

args

)

       

{

           

int

a

=

21

;

           

int

b

=

10

;

           

int

c

;

            c

=

a

b

;

            Console

.

WriteLine

(

"Line 1 - value of c is {0}"

, c

)

;

            c

=

a

b

;

            Console

.

WriteLine

(

"Line 2 - value of c is {0}"

, c

)

;

            c

=

a

b

;

            Console

.

WriteLine

(

"Line 3 - value of c is {0}"

, c

)

;

            c

=

a

/

b

;

            Console

.

WriteLine

(

"Line 4 - value of c is {0}"

, c

)

;

            c

=

a

%

b

;

            Console

.

WriteLine

(

"Line 5 - value of c is {0}"

, c

)

;

           

// ++a increments first, then assigns

            c

=

++

a

;

            Console

.

WriteLine

(

"Line 6 - value of c is {0}"

, c

)

;

           

// At this point the value of a is 22

           

// --a decrements first, then assigns

            c

=

--

a

;

            Console

.

WriteLine

(

"Line 7 - value of c is {0}"

, c

)

;

            Console

.

ReadLine

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



Line 1 - c 的值是 31

Line 2 - c 的值是 11

Line 3 - c 的值是 210

Line 4 - c 的值是 2

Line 5 - c 的值是 1

Line 6 - c 的值是 22

Line 7 - c 的值是 21

Example

using

System

;

namespace

OperatorsAppl

{

   

class

Program

   

{

       

static

void

Main

(

string

[

]

args

)

       

{

           

int

a

=

1

;

           

int

b

;

           

// a++ assigns first, then increments

            b

=

a

++;

            Console

.

WriteLine

(

"a = {0}"

, a

)

;

            Console

.

WriteLine

(

"b = {0}"

, b

)

;

            Console

.

ReadLine

(

)

;

           

// ++a increments first, then assigns

            a

=

1

;

// Reinitialize a

            b

=

++

a

;

            Console

.

WriteLine

(

"a = {0}"

, a

)

;

            Console

.

WriteLine

(

"b = {0}"

, b

)

;

            Console

.

ReadLine

(

)

;

           

// a-- assigns first, then decrements

            a

=

1

;

 

// Reinitialize a

            b

=

a

--;

            Console

.

WriteLine

(

"a = {0}"

, a

)

;

            Console

.

WriteLine

(

"b = {0}"

, b

)

;

            Console

.

ReadLine

(

)

;

           

// --a decrements first, then assigns

            a

=

1

;

 

// Reinitialize a

            b

=

--

a

;

            Console

.

WriteLine

(

"a = {0}"

, a

)

;

            Console

.

WriteLine

(

"b = {0}"

, b

)

;

            Console

.

ReadLine

(

)

;

       

}

   

}

}

Run Example »

Running the above program, the output is:



a = 2

b = 1

a = 2

b = 2

a = 0

b = 1

a = 0

b = 0

Relational operators

The following table shows all relational operators supported by C#. Assume variable A has the value 10, and variable B has the value 20, then:

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

Example

Look at the following example to understand all available relational operators in C#:

Example

using

System

;

class

Program

{

 

static

void

Main

(

string

[

]

args

)

 

{

     

int

a

=

21

;

     

int

b

=

10

;

     

     

if

(

a

==

b

)

     

{

          Console

.

WriteLine

(

"Line 1 - a equals b"

)

;

     

}

     

else

     

{

          Console

.

WriteLine

(

"Line 1 - a does not equal b"

)

;

     

}

     

if

(

a

<b){Console>

.

WriteLine

(

"Line 2 - a is less than b"

)

;

     

}

     

else

     

{

          Console

.

WriteLine

(

"Line 2 - a is not less than b"

)

;

     

}

     

if

(

a

b

)

     

{

          Console

.

WriteLine

(

"Line 3 - a is greater than b"

)

;

     

}

     

else

     

{

          Console

.

WriteLine

(

"Line 3 - a is not greater than b"

)

;

     

}

     

/* Change the values of a and b */

      a

=

5

;

      b

=

20

;

     

if

(

a

<=

b

)

     

{

         Console

.

WriteLine

(

"Line 4 - a is less than or equal to b"

)

;

     

}

     

if

(

b

=

a

)

     

{

         Console

.

WriteLine

(

"Line 5 - b is greater than or equal to a"

)

;

     

}

 

}

}

When the code above is compiled and executed, it produces the following result:



Line 1 - a 不等于 b

Line 2 - a 不小于 b

Line 3 - a 大于 b

Line 4 - a 小于或等于 b

Line 5 - b 大于或等于 a

Logical operators

The following table shows all logical operators supported by C#. Assume variable A is the boolean value true, and variable B is the boolean value false, then:

operator Description Example
&& is called the logical AND operator. If both operands are non-zero, the condition is true. (A && B) is false.
|| is called the logical OR operator. If either of the two operands is non-zero, the condition is true. (A || B) is true.
! is called the logical NOT operator. It is used to reverse the logical state of its operand. If a condition is true, the logical NOT operator will make it false. !(A && B) is true.

Example

Look at the following example to understand all available logical operators in C#:

Example

using

System

;

namespace

OperatorsAppl

{

   

class

Program

   

{

       

static

void

Main

(

string

[

]

args

)

       

{

           

bool

a

=

true

;

           

bool

b

=

true

;

           

           

if

(

a

&&

b

)

           

{

               Console

.

WriteLine

(

"Line 1 - condition is true"

)

;

           

}

           

if

(

a

||

b

)

           

{

                Console

.

WriteLine

(

"Line 2 - condition is true"

)

;

           

}

           

/* Change the values of a and b */

            a

=

false

;

            b

=

true

;

           

if

(

a

&&

b

)

           

{

                Console

.

WriteLine

(

"Line 3 - condition is true"

)

;

           

}

           

else

           

{

                Console

.

WriteLine

(

"Line 3 - condition is not true"

)

;

           

}

           

if

(

!

(

a

&&

b

)

)

           

{

                Console

.

WriteLine

(

"Line 4 - condition is true"

)

;

           

}

            Console

.

ReadLine

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



Line 1 - 条件为真

Line 2 - 条件为真

Line 3 - 条件不为真

Line 4 - 条件为真

Bitwise operators

Bitwise operators act on bits and perform operations bit by bit. The truth tables for &, |, and ^ are shown below:

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume A = 60 and B = 13; represented in binary format, they are as follows:

A = 0011 1100

B = 0000 1101


A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

The following table lists the bitwise operators supported by C#. Assume variable A has a value of 60, variable B has a value of 13, then:

operator Description Example
& If present in both operands, the binary AND operator copies a bit to the result. (A & B) will give 12, which is 0000 1100
| If present in either operand, the binary OR operator copies a bit to the result. (A | B) will give 61, which is 0011 1101
^ If a bit is present in one operand but not both, the binary XOR operator copies that bit to the result. (A ^ B) gives 49, which is 0011 0001
~ The bitwise NOT operator is a unary operator with a "flip" bit effect: 0 becomes 1 and 1 becomes 0, including the sign bit. (~A ) will give -61, which is 1100 0011, the two's complement form of a signed binary number.
<< Binary left shift operator. The value of the left operand is shifted left by the number of bits specified by the right operand. A << 2 will give 240, which is 1111 0000
>> Binary right shift operator. The value of the left operand is shifted right by the number of bits specified by the right operand. A >> 2 will give 15, which is 0000 1111

Example

Look at the following example to understand all available bitwise operators in C#:

Example

using

System

;

namespace

OperatorsAppl

{

   

class

Program

   

{

       

static

void

Main

(

string

[

]

args

)

       

{

           

int

a

=

60

;

           

/* 60 = 0011 1100 */

 

           

int

b

=

13

;

           

/* 13 = 0000 1101 */

           

int

c

=

0

;

         

             c

=

a

&

b

;

         

/* 12 = 0000 1100 */

             Console

.

WriteLine

(

"Line 1 - value of c is {0}"

, c

)

;

             c

=

a

|

b

;

         

/* 61 = 0011 1101 */

             Console

.

WriteLine

(

"Line 2 - value of c is {0}"

, c

)

;

             c

=

a

^

b

;

         

/* 49 = 0011 0001 */

             Console

.

WriteLine

(

"Line 3 - value of c is {0}"

, c

)

;

             c

=

~a

;

             

/*-61 = 1100 0011 */

             Console

.

WriteLine

(

"Line 4 - value of c is {0}"

, c

)

;

             c

=

a

<<

2

;

   

/* 240 = 1111 0000 */

             Console

.

WriteLine

(

"Line 5 - value of c is {0}"

, c

)

;

             c

=

a

2

;

   

/* 15 = 0000 1111 */

             Console

.

WriteLine

(

"Line 6 - value of c is {0}"

, c

)

;

            Console

.

ReadLine

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



Line 1 - c 的值是 12

Line 2 - c 的值是 61

Line 3 - c 的值是 49

Line 4 - c 的值是 -61

Line 5 - c 的值是 240

Line 6 - c 的值是 15

Assignment operators

The following table lists the assignment operators supported by C#:

operator Description Example
= Simple assignment operator, assigns the value of the right operand to the left operand C = A + B will assign the value of A + B to C
+= Add-and-assign operator, assigns the result of adding the right operand to the left operand to the left operand C += A is equivalent to C = C + A
-= Subtract-and-assign operator, assigns the result of subtracting the right operand from the left operand to the left operand C -= A is equivalent to C = C - A
*= Multiply-and-assign operator, assigns the result of multiplying the right operand by the left operand to the left operand C *= A is equivalent to C = C * A
/= Divide-and-assign operator, assigns the result of dividing the left operand by the right operand to the left operand C /= A is equivalent to C = C / A
%= Modulus-and-assign operator, assigns the modulus of the two operands to the left operand C %= A is equivalent to C = C % A
<<= Left-shift-and-assign operator C <<= 2 is the same as C = C << 2
>>= Right-shift-and-assign operator C >>= 2 is the same as C = C >> 2
&= Bitwise AND-and-assign operator C &= 2 is the same as C = C & 2
^= Bitwise XOR-and-assign operator C ^= 2 is the same as C = C ^ 2
|= Bitwise OR-and-assign operator C |= 2 is the same as C = C | 2

Example

Look at the following example to understand all available assignment operators in C#:

Example

using

System

;

namespace

OperatorsAppl

{

   

class

Program

   

{

       

static

void

Main

(

string

[

]

args

)

       

{

           

int

a

=

21

;

           

int

c

;

            c

=

a

;

            Console

.

WriteLine

(

"Line 1 - = value of c = {0}"

, c

)

;

            c

+=

a

;

            Console

.

WriteLine

(

"Line 2 - += value of c = {0}"

, c

)

;

            c

-=

a

;

            Console

.

WriteLine

(

"Line 3 - -= value of c = {0}"

, c

)

;

            c

*=

a

;

            Console

.

WriteLine

(

"Line 4 - *= value of c = {0}"

, c

)

;

            c

/=

a

;

            Console

.

WriteLine

(

"Line 5 - /= value of c = {0}"

, c

)

;

            c

=

200

;

            c

%=

a

;

            Console

.

WriteLine

(

"Line 6 - %= value of c = {0}"

, c

)

;

            c

<<=

2

;

            Console

.

WriteLine

(

"Line 7 - <<= value of c = {0}"

, c

)

;

            c

=

2

;

            Console

.

WriteLine

(

"Line 8 - >>= value of c = {0}"

, c

)

;

            c

&=

2

;

            Console

.

WriteLine

(

"Line 9 - &= value of c = {0}"

, c

)

;

            c

^=

2

;

            Console

.

WriteLine

(

"Line 10 - ^= value of c = {0}"

, c

)

;

            c

|=

2

;

            Console

.

WriteLine

(

"Line 11 - |= value of c = {0}"

, c

)

;

            Console

.

ReadLine

(

)

;

       

}

   

}

}

When the code above is compiled and executed, it produces the following result:



Line 1 - =     c 的值 = 21

Line 2 - +=    c 的值 = 42

Line 3 - -=    c 的值 = 21

Line 4 - *=    c 的值 = 441

Line 5 - /=    c 的值 = 21

Line 6 - %=    c 的值 = 11

Line 7 - <<=    c 的值 = 44

Line 8 - >>=    c 的值 = 11

Line 9 - &=    c 的值 = 2

Line 10 - ^=    c 的值 = 0

Line 11 - |=    c 的值 = 2

Other operators

The following table lists some other important operators supported by C#, including sizeof、typeof and ? :。

operator Description Example
sizeof() returns the size of a data type. sizeof(int) returns 4.
typeof() returns the type of a class. typeof(StreamReader);
& Returns the address of a variable. &a; gives the actual address of the variable.
* pointer to a variable. *a; will point to a variable.
? : Conditional expression If the condition is true ? then X : otherwise Y
is determines whether an object is of a certain type. If( Ford is Car) // checks whether Ford is an object of the Car class.
as explicit cast; even if the conversion fails, no exception is thrown. Object obj = new StringReader("Hello");
StringReader r = obj as StringReader;

Example

Example

using

System

;

namespace

OperatorsAppl

{

   

   

class

Program

   

{

     

static

void

Main

(

string

[

]

args

)

     

{

         

         

/* Example of the sizeof operator */

         Console

.

WriteLine

(

"The size of int is {0}"

,

sizeof

(

int

)

)

;

         Console

.

WriteLine

(

"The size of short is {0}"

,

sizeof

(

short

)

)

;

         Console

.

WriteLine

(

"The size of double is {0}"

,

sizeof

(

double

)

)

;

         

         

/* Example of the ternary operator */

         

int

a, b

;

         a

=

10

;

         b

=

(

a

==

1

)

?

20

:

30

;

         Console

.

WriteLine

(

"The value of b is {0}"

, b

)

;

         b

=

(

a

==

10

)

?

20

:

30

;

         Console

.

WriteLine

(

"The value of b is {0}"

, b

)

;

         Console

.

ReadLine

(

)

;

     

}

   

}

}

When the code above is compiled and executed, it produces the following result:



int 的大小是 4

short 的大小是 2

double 的大小是 8

b 的值是 30

b 的值是 20

The typeof keyword is used to obtain the Type object of a type; it is commonly used in reflection and dynamic creation of type instances.

The following is a simple example using typeof:

Example

using

System

;

class

Program

{

   

static

void

Main

(

string

[

]

args

)

   

{

        Type type

=

typeof

(

string

)

;

        Console

.

WriteLine

(

type

.

FullName

)

;

        Console

.

ReadKey

(

)

;

   

}

}

In the code above, we use the typeof keyword to obtain the Type object of the string type and store it in a variable of type Type named type. Then we use the FullName property to print the fully qualified name of the type.

When the code above is compiled and executed, it produces the following result:

System.String

Operator Precedence in C#

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Some operators have higher precedence than others; for example, the multiplication and division operators have higher precedence than the addition and subtraction operators.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20, because the * operator has higher precedence than +, so the multiplication 3*2 is computed first, then 7 is added.

The table below lists the operators from highest to lowest precedence. Operators with higher precedence appear at the top of the table, and those with lower precedence appear at the bottom. In an expression, operators with higher precedence are evaluated first.

A Simple Summary of Precedence

: parentheses first, then multiplication and division before addition and subtraction, then shifts before relational, and logical before conditional, and finally the comma

,

。

Category  operator  Associativity 
Postfix  () [] -> . ++ - -   Left to right 
Unary  + - ! ~ ++ - - (type)* & sizeof  Right to left 
Multiplicative  * / %  Left to right 
Additive  + -  Left to right 
Shift  << >>  Left to right 
Relational  < <= > >=  Left to right 
Equality  == !=  Left to right 
Bitwise AND  &  Left to right 
Bitwise XOR  ^  Left to right 
Bitwise OR  |  Left to right 
Logical AND  &&  Left to right 
Logical OR  ||  Left to right 
Conditional  ?:  Right to left 
Assignment  = += -= *= /= %=>>= <<= &= ^= |=  Right to left 
Comma  ,  Left to right 

Example

Example

using

System

;

namespace

OperatorsAppl

{

   

   

class

Program

   

{

     

static

void

Main

(

string

[

]

args

)

     

{

         

int

a

=

20

;

         

int

b

=

10

;

         

int

c

=

15

;

         

int

d

=

5

;

         

int

e

;

         e

=

(

a

b

)

c

/

d

;

   

// ( 30 * 15 ) / 5

         Console

.

WriteLine

(

"The value of (a + b) * c / d is {0}"

, e

)

;

         e

=

(

(

a

b

)

c

)

/

d

;

 

// (30 * 15 ) / 5

         Console

.

WriteLine

(

"The value of ((a + b) * c) / d is {0}"

, e

)

;

         e

=

(

a

b

)

(

c

/

d

)

;

 

// (30) * (15/5)

         Console

.

WriteLine

(

"The value of (a + b) * (c / d) is {0}"

, e

)

;

         e

=

a

(

b

c

)

/

d

;

   

//  20 + (150/5)

         Console

.

WriteLine

(

"The value of a + (b * c) / d is {0}"

, e

)

;

         Console

.

ReadLine

(

)

;

     

}

   

}

}

When the code above is compiled and executed, it produces the following result:



(a + b) * c / d 的值是 90

((a + b) * c) / d 的值是 90

(a + b) * (c / d) 的值是 90

a + (b * c) / d 的值是 50

Precedence of Arithmetic and Assignment Operators:

Example

int

a

=

10

, b

=

5

, c

=

2

;

int

result

=

a

b

c

;

 

// Multiplication is computed first, then addition

Console

.

WriteLine

(

result

)

;

 

// Output 20

result

=

(

a

b

)

c

;

   

// Addition is computed first, then multiplication

Console

.

WriteLine

(

result

)

;

 

// Output 30

a

+=

b

c

;

             

// Multiplication is computed first, then addition assignment is performed

Console

.

WriteLine

(

a

)

;

     

// Output 20

Precedence of Logical Operators:

Example

bool

a

=

true

, b

=

false

, c

=

true

;

bool

result

=

a

||

b

&&

c

;

 

// AND is computed first, then OR

Console

.

WriteLine

(

result

)

;

 

// Output true

result

=

(

a

||

b

)

&&

c

;

   

// OR is computed first, then AND

Console

.

WriteLine

(

result

)

;

 

// Output true

result

=

a

||

(

b

&&

c

)

;

   

// AND is computed first, then OR

Console

.

WriteLine

(

result

)

;

 

// Output true

Precedence of Conditional Operators:

Example

int

a

=

10

, b

=

5

;

string

result

=

a

b

?

"a is greater than b"

:

"a is not greater than b"

;

 

// The size relationship is evaluated first, then the conditional statement is executed

Console

.

WriteLine

(

result

)

;

 

// Output "a is greater than b"

**Note:** Since parentheses can change operator precedence, it is recommended to use parentheses whenever possible in practice to make the order of operations explicit and improve code readability and accuracy.