English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Operators

C# 面向对象(OOP)+C#中的运算符是对操作数执行某些操作的一些特殊符号。在数学中,加号(

)is the sum of the left and right numbers. Similarly, C# includes operators of different types of operations.

  • Arithmetic operators

  • Relational operators

  • Logical operators

  • Bitwise operators

  • Assignment operator

  • Other operators

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

Arithmetic operators

The following table shows all the arithmetic operators supported by C#. Assume the variable = A is equivalent to C = C The value is 10, variable B The value is 20, then:

OperatorsDescriptionExample
+Add the two operands= A is equivalent to C = C + B will be 30
-Subtract the second operand from the first operand= A is equivalent to C = C - B will be -10
*Multiply the two operands= A is equivalent to C = C * B will be 200
/The numerator is divided by the denominatorB / A will be 2
%Modulus operator, the remainder after divisionB % A will result in 0
++Increment operator, the integer value is increased 1= A is equivalent to C = C++ will be obtained 11
--Decrement operator, the integer value is reduced 1= A is equivalent to C = C-- will be obtained 9

Online Example

Please see the following examples to understand all the available arithmetic operators in C#:

using System;
namespace OperatorsAppl
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 21;
            int b = 10;
            Please see the following example to understand all the available assignment operators in C#:
            c = a + b;
            Console.WriteLine("Line 1 - The value of c is {0}
            c = a - b;
            Console.WriteLine("Line 2 - The value of c is {0}
            c = a * b;
            Console.WriteLine("Line 3 - The value of c is {0}
            c = a / b;
            Console.WriteLine("Line 4 - The value of c is {0}
            c = a % b;
            Console.WriteLine("Line 5 - The value of c is {0}
            // ++a is incremented first and then assigned
            =  c 的价值 = {0} ++a;
            Console.WriteLine("Line 6 - The value of c is {0}
            // At this point, the value of a is 22
            // --a is decremented first and then assigned
            =  c 的价值 = {0} --a;
            Console.WriteLine("Line 7 - The value of c is {0}
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - The value of c is 31
Line 2 - The value of c is 11
Line 3 - The value of c is 210
Line 4 - The value of c is 2
Line 5 - The value of c is 1
Line 6 - The value of c is 22
Line 7 - The value of c is 21
  • c = a++: First, assign a to c, then increment a.

  • c = ++a: First, increment a, then assign a to c.

  • c = a--: First, assign a to c, then decrement a.

  • c = --a: First, decrement a, then assign a to c.

using System;
namespace OperatorsAppl
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b;
            // a++ Assigned first, then incremented
            b = a++;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();
            // ++a is incremented first and then assigned
            a = 1; // Reinitialize a
            b = ++a;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();
            // a-- Assigned first, then decremented
            a = 1;  // Reinitialize a
            b = a--;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();
            // --a is decremented first and then assigned
            a = 1;  // Reinitialize a
            b = --a;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();
        }
    }
}

After executing the above program, the output result is:

a = 2
b = 1
a = 2
b = 2
a = 0
b = 1
a = 0
b = 0

Relational operators

The following table shows all the relational operators supported by C#. Assume the variable = A is equivalent to C = C The value is 10, variable B The value is 20, then:

OperatorsDescriptionExample
==It checks if the values of the two operands are equal, and if equal, the condition is true.(A == B) is not true.
!=It checks if the values of the two operands are equal, and if not equal, the condition is true.(A != B) is true.
>It checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true.(A > B) is not true.
<It checks if the value of the left operand is less than the value of the right operand, and if so, the condition is true.(A < B) is true.
>=It checks if the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition is true.(A >= B) is not true.
<=It checks if the value of the left operand is less than or equal to the value of the right operand, and if so, the condition is true.(A <= B) is true.

Online Example

Please see the following examples to understand all the available relational operators in C#:

Online Example

using System;
class Program
{
  static void Main(string[] args)
  {
      int a = 21;
      int b = 10;
      
      if (a == b)
      {
          Console.WriteLine("Line 1 - a is equal to b);
      }
      else
      {
          Console.WriteLine("Line 1 - a is not equal to 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 above code is compiled and executed, it will produce the following results:

Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is less than or equal to b
Line 5 - b is greater than or equal to a

Logical operators

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

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

Online Example

Please see the following examples to understand all the available logical operators in C#:

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 - The condition is not true);
            }
            if (!(a && b))
            {
                Console.WriteLine("Line 4 - Condition is true
            }
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

Bitwise operators

Bitwise operators operate on bits and perform operations bit by bit. The truth tables for &、 | and ^ are as follows:

pqp & qp | qp ^ q
00000
01011
11110
10011

Assume if A = 60, and B = 13, now 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 the variable = A is equivalent to C = C The value is 60, variable B The value is 13, then:

OperatorsDescriptionExample
&Binary AND operator copies a bit to the result if it exists in both operands.(A & B) will be obtained 12, which is 0000 1100
|Binary OR operator copies a bit to the result if it exists in any operand.(A | B) will be obtained 61, which is 0011 1101
^Binary XOR operator copies a bit to the result if it exists in one operand but not in both.(A ^ B) will be obtained 49, which is 0011 0001
~The bitwise NOT operator is a unary operator with a 'flip' bit effect, that is, 0 becomes1,1Turned to 0, including the sign bit.(~A) will be obtained -61, which is 1100 0011, which is the two's complement form of a signed binary number.
<<Binary left shift operator. The value of the left operand is shifted to the left by the number of bits specified by the right operand.A << 2 will be obtained 240, which is 1111 0000
>>Binary right shift operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand.A >> 2 will be obtained 15, which is 0000 1111

Online Example

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

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 - The value of c is {0}
             c = a | b;           /* 61 = 0011 1101 */
             Console.WriteLine("Line 2 - The value of c is {0}
             c = a ^ b;           /* 49 = 0011 0001 */
             Console.WriteLine("Line 3 - The value of c is {0}
             c = ~a;               /*-61 = 1100 0011 */
             Console.WriteLine("Line 4 - The value of c is {0}
             c = a << 2;     /* 240 = 1111 0000 */
             Console.WriteLine("Line 5 - The value of c is {0}
             c = a >> 2;     /* 15 = 0000 1111 */
             Console.WriteLine("Line 6 - The value of c is {0}
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it will produce the following results:

Line 1 - The value of c is 12
Line 2 - The value of c is 61
Line 3 - The value of c is 49
Line 4 - The value of c is -61
Line 5 - The value of c is 240
Line 6 - The value of c is 15

Assignment operator

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

OperatorsDescriptionExample
=Simple assignment operator, assigns the value of the right operand to the left operandC = A + B will assign A + The value of B is assigned to C
+=The result of adding the right operand to the left operand is assigned to the left operand using the addition assignment operator. +C + = A is equivalent to C = C
-=The result of subtracting the right operand from the left operand is assigned to the left operand using the subtraction assignment operator. -C - = A is equivalent to C = C
*=The result of multiplying the right operand by the left operand is assigned to the left operand using the multiplication assignment operator. *C * = A is equivalent to C = C
/=The result of dividing the left operand by the right operand is assigned to the left operand using the division assignment operator. /C / = A is equivalent to C = C
A%=Modulus and assignment operator, assigns the modulus of two operands to the left operand
C %= A is equivalent to C = C % A<<=Left shift and assignment operator 2 C <<= 2
It is equivalent to C = C <<>>=Right shift and assignment operator 2 C >>= 2
It is equivalent to C = C >>&=Bitwise AND and assignment operator 2 C &= 2
It is equivalent to C = C &^=Bitwise XOR and assignment operator 2 C ^= 2
It is equivalent to C = C ^|=Bitwise OR and assignment operator 2 C |= 2

Online Example

It is equivalent to C = C |

using System;
namespace OperatorsAppl
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 21;
            Please see the following example to understand all the available assignment operators in C#:
            int c;
            Console.WriteLine("Line 1 - = a;
            c += c 的价值 = {0}
            Console.WriteLine("Line 2 - +c = a;
            c -= c 的价值 = {0}
            Console.WriteLine("Line 3 - -= a;
            c *= c 的价值 = {0}
            Console.WriteLine("Line 4 - *= a;
            c /= c 的价值 = {0}
            Console.WriteLine("Line 5 - /= a;
            =  c 的价值 = {0} 2c =
            00;
            Console.WriteLine("Line 6 - c %=
            %=  c 的价值 = {0} 2;
            Console.WriteLine("Line 7 - c <<=
            <<=  c 的价值 = {0} 2;
            Console.WriteLine("Line 8 - c >>=
            >>=  c 的价值 = {0} 2;
            Console.WriteLine("Line 9 - c &=
            &=  c 的价值 = {0} 2;
            Console.WriteLine("Line 10 - c ^=
            ^=  c 的价值 = {0} 2;
            Console.WriteLine("Line 11 - |=  c 的价值 = {0}
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it will produce the following results:

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 sizeoftypeof and ? :.

OperatorsDescriptionExample
sizeof()Return the size of the data type.sizeof(int), will return 4.
typeof()Return the type of the class.typeof(StreamReader);
&Return the address of the variable.&a; will get the actual address of the variable.
*Pointer of a variable.*a; will point to a variable.
? :Conditional expressionIf the condition is true ? then it is X : otherwise it is Y
isDetermine whether an object is of a certain type.If( Ford is Car)  // Check if Ford is an object of the Car class.
asForced conversion, even if the conversion fails, no exception will be thrown.Object obj = new StringReader("Hello");
   StringReader r = obj as StringReader;

Online Example

using System;
namespace OperatorsAppl
{
    
   class Program
   {
      static void Main(string[] args)
      {
         
         /* Example of 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 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 above code is compiled and executed, it will produce the following results:

The size of int is 4
The size of short is 2
The size of double is 8
The value of b is 30
The value of b is 20

Operator precedence in C#

The precedence of operators determines how terms are combined in an expression. This affects how an expression is calculated. Some operators have higher precedence than others, for example, multiplication and division operators have higher precedence than addition and subtraction operators.

For example x = 7 + 3 * 2, here, x is assigned the value of 13, not 20, because the operator * than + higher precedence, so multiplication is calculated first 3*2Then, in addition to 7.

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

Categories Operators Associativity 
Postfix () [] -> .  ++   - -  From left to right 
Unary +  -   ! ~  ++  - -   (type)*  & sizeof From right to left 
Multiplication and Division *  /  % From left to right 
Addition and Subtraction +  - From left to right 
Shift << >> From left to right 
Relation < <= > >= From left to right 
Equality == != From left to right 
Bitwise AND & From left to right 
Bitwise XOR ^ From left to right 
Bitwise OR | From left to right 
Logical AND && From left to right 
Logical OR || From left to right 
Conditional ?: From right to left 
Assignment =  +=  -=  *=  /= %=>>= <<= &= ^= |= From right to left 
Comma , From left to right 

Online 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("(a + b) * c / The value of d is {0}, e);
         e = ((a + b) * c) / d;   // (30 * 15 ) / 5
         Console.WriteLine("((a + b) * c) / The value of d is {0}, e);
         e = (a + b) * (c / d);   // (30) * (15/5)
         Console.WriteLine("(a + b) * (c / The value of d is {0}, e);
         e = a + (b * c) / d;    //  20 + (150/5)
         Console.WriteLine("a + (b * c) / The value of d is {0}, e);
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it will produce the following results:

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