Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The operator for C


May 11, 2021 C#


Table of contents


The operator of the C#

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. There are a wealth of built-in operators, divided into six categories:

  • Arithmetic operator
  • The relationship operator
  • The logical operator
  • Bit operator
  • The assignment operator
  • Miscellaneous operator

This tutorial will cover arithmetic operators, relationship operators, logical operators, bit operators, assignment operators, and other operators one by one.

Arithmetic operator

The following table shows all the arithmetic operators supported by C#. Assuming that the value of variable A is 10 and the value of variable B is 20, then:

Operator describe Example
+ Add two operands A + B will get 30
- Split the second operand from the first operand A - B will get -10
* Multiply two operands A * B will get 200
/ Molecule division B / A will get 2
% Take an operator, the remainder after the removal B% a will get 0
++ Self-incremental operator, integer value increase 1 A ++ will get 11
-- Self-reduction operator, integer value reduction 1 A - will get 9

Instance

Take a look at the following example to learn about all the arithmetic operators available in C#:

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 - c 的值是 {0}", c);
            c = a - b;
            Console.WriteLine("Line 2 - c 的值是 {0}", c);
            c = a * b;
            Console.WriteLine("Line 3 - c 的值是 {0}", c);
            c = a / b;
            Console.WriteLine("Line 4 - c 的值是 {0}", c);
            c = a % b;
            Console.WriteLine("Line 5 - c 的值是 {0}", c);
            c = a++;
            Console.WriteLine("Line 6 - c 的值是 {0}", c);
            c = a--;
            Console.WriteLine("Line 7 - c 的值是 {0}", c);
            Console.ReadLine();
        }
    }
}

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

Line 1 - c 的值是 31
Line 2 - c 的值是 11
Line 3 - c 的值是 210
Line 4 - c 的值是 2
Line 5 - c 的值是 1
Line 6 - c 的值是 21
Line 7 - c 的值是 22

The relationship operator

The following table shows all the relationship operators supported by C#. Assuming that the value of variable A is 10 and the value of variable B is 20, then:

Operator describe Example
== Check if the value of the two operands is equal, if equally, the condition is true. (A == b) is not true.
!= Check if the value of the two operands is equal, if it is not equal, the condition is true. (A! = B) is true.
> Check if the value of the left operand is greater than the value of the right operation, if it is true. (A> b) is not true.
< Check if the value of the left operand is less than the value of the right operand, if it is true. (A <b) is true.
>= Check if the value of the left operand is greater than or equal to the value of the right operand, if it is true. (A> = b) is not true.
<= Check if the value of the left operand is less than or equal to the value of the right operand, if it is true. (A <= b) is true.

Instance

Take a look at the following example to learn about all the available relationship operators in C#:

using System;

class Program
{
  static void Main(string[] args)
  {
      int a = 21;
      int b = 10;
      
      if (a == b)
      {
          Console.WriteLine("Line 1 - a 等于 b");
      }
      else
      {
          Console.WriteLine("Line 1 - a 不等于 b");
      }
      if (a < b)       {           Console.WriteLine("Line 2 - a 小于 b");       }       else       {           Console.WriteLine("Line 2 - a 不小于 b");       }       if (a > b)
      {
          Console.WriteLine("Line 3 - a 大于 b");
      }
      else
      {
          Console.WriteLine("Line 3 - a 不大于 b");
      }
      /* 改变 a 和 b 的值 */
      a = 5;
      b = 20;
      if (a <= b)       {          Console.WriteLine("Line 4 - a 小于或等于 b");       }       if (b >= a)
      {
         Console.WriteLine("Line 5 - b 大于或等于 a");
      }
  }
}

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

Line 1 - a 不等于 b
Line 2 - a 不小于 b
Line 3 - a 大于 b
Line 4 - a 小于或等于 b
Line 5 - b 大于或等于 a

The logical operator

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

Operator describe Example
&& Logic and operators.If the two operands are not zero, the conditions are true. (A && b) is false.
|| Logic or operator.If there is any non-zero in the two operands, the condition is true. (A || b) is true.
! Logical non-operator.The logic state used to reverse the operand.If the condition is true, the logical non-operator will make it fake. ! (A && b) is true.

Instance

Take a look at the following example to learn about all the logical operators available 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 - 条件为真");
            }
            if (a || b)
            {
                Console.WriteLine("Line 2 - 条件为真");
            }
            /* 改变 a 和 b 的值 */
            a = false;
            b = true;
            if (a && b)
            {
                Console.WriteLine("Line 3 - 条件为真");
            }
            else
            {
                Console.WriteLine("Line 3 - 条件不为真");
            }
            if (!(a && b))
            {
                Console.WriteLine("Line 4 - 条件为真");
            }
            Console.ReadLine();
        }
    }
}

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

Line 1 - 条件为真
Line 2 - 条件为真
Line 3 - 条件不为真
Line 4 - 条件为真

Bit operator

Bit operators act on bits and perform operations bit by bit. & amp;、 | The true value tables for and s are as follows:

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

Suppose that if A is 60 and B is 13, they are now represented in binary format, 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 bit operators that are supported by C#. Assuming that the value of variable A is 60 and the value of variable B is 13, then:

Operator describe Example
& If existing in two operands, the binary and operator replicates one bit to the result. (A & B) will get 12, ie 0000 1100
| If there is in any operand, the binary OR operator replicates one bit to the result. (A | b) will get 61, ie 0011 1101
^ If present in one of the operands but there is different, the binary or operator replicates one bit to the result. (A ^ b) will get 49, ie 0011 0001
~ The binary complement operator is a one-dollar operator with the "flip" bit effect. (~ A) will result in -61, which is a complement form of 1100 0011, 2, a binary number with a symbol.
<< Binary left shift operator.The value of the left operand moves to the left move the number of bits specified by the right operand. A << 2 will get 240, that is, 1111 0000
>> Binary right shift operator.The value of the left operation is moved to the right number of bits specified by the right operand. A >> 2 will get 15, ie 0000 1111

Instance

Take a look at the following example to learn about all the bit 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 - c 的值是 {0}", c );

             c = a | b;           /* 61 = 0011 1101 */
             Console.WriteLine("Line 2 - c 的值是 {0}", c);

             c = a ^ b;           /* 49 = 0011 0001 */
             Console.WriteLine("Line 3 - c 的值是 {0}", c);

             c = ~a;               /*-61 = 1100 0011 */
             Console.WriteLine("Line 4 - c 的值是 {0}", c);

             c = a << 2;     /* 240 = 1111 0000 */         
             Console.WriteLine("Line 5 - c 的值是 {0}", c);       
             c = a >> 2;     /* 15 = 0000 1111 */
             Console.WriteLine("Line 6 - c 的值是 {0}", c);
            Console.ReadLine();
        }
    }
}

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

Line 1 - c 的值是 12
Line 2 - c 的值是 61
Line 3 - c 的值是 49
Line 4 - c 的值是 -61
Line 5 - c 的值是 240
Line 6 - c 的值是 15

The assignment operator

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

Operator describe Example
= Simple assignment operator, assign the value of the right operand to the left operand C = a + b will assign the value of the A + B to C
+= Adding the operator, add the result of the left operand to the left operand to the left operand C + = a corresponds to c = c + a
-= Reduce the value, subtract the left operand to the left operand to the left operand C - = a equivalent to c = c - a
*= Multiplication and assignment operators, multiply the right operation number by the result of the left operand to the left operand C * = a equivalent to c = c * a
/= Except and assign the operator, divide the left operand to assign the left operand to the left operand C / = a corresponds to c = c / a
%= Dammon and assign the operator, ask the two operands to give the left operand C% = a corresponds to c = c% a
<<= Left and assignment operator C << = 2 equivalent to C = c << 2
>>= Right shift and assignment operator C >> = 2 is equivalent to C = C >> 2
&= Bouting and assignment operator C & = 2 is equivalent to C = C & 2
^= Press or assignment operator C ^ = 2 is equivalent to C = C ^ 2
|= Bit or be assigned operators C | = 2 equivalent to C = C | 2

Instance

Take a look at the following example to learn about all the available assignment operators in C#:

using System;

namespace OperatorsAppl
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 21;
            int c;

            c = a;
            Console.WriteLine("Line 1 - =  c 的值 = {0}", c);

            c += a;
            Console.WriteLine("Line 2 - += c 的值 = {0}", c);

            c -= a;
            Console.WriteLine("Line 3 - -=  c 的值 = {0}", c);

            c *= a;
            Console.WriteLine("Line 4 - *=  c 的值 = {0}", c);

            c /= a;
            Console.WriteLine("Line 5 - /=  c 的值 = {0}", c);

            c = 200;
            c %= a;
            Console.WriteLine("Line 6 - %=  c 的值 = {0}", c);

            c <<= 2;      
            Console.WriteLine("Line 7 - <<=  c 的值 = {0}", c);     
            c >>= 2;
            Console.WriteLine("Line 8 - >>=  c 的值 = {0}", c);

            c &= 2;
            Console.WriteLine("Line 9 - &=  c 的值 = {0}", c);

            c ^= 2;
            Console.WriteLine("Line 10 - ^=  c 的值 = {0}", c);

            c |= 2;
            Console.WriteLine("Line 11 - |=  c 的值 = {0}", c);
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces 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

Miscellaneous operator

The following table lists some of the other important operators supported by C#, including sizeof, typeof, ? : .

The operator describes the size of the data type returned by the instance sizeof(). s izeof(int), which returns 4. T ypeof() returns the type of class. t ypeof(StreamReader); T he address of the variable is returned. & amp;a; T he actual address of the variable is obtained. T he pointer to the variable. * a; w ill point to a variable. : Conditional expression If the condition is true ? X : Otherwise, Y is to determine whether the object is of a certain type. I f (Ford is Car)// Check if Ford is an object of the Car class. a s cast, an exception is not thrown even if the conversion fails. Object obj = new StringReader("Hello");


StringReader r = obj as StringReader;

Instance

using System;

namespace OperatorsAppl
{
    
   class Program
   {
      static void Main(string[] args)
      {
         
         /* sizeof 运算符的实例 */
         Console.WriteLine("int 的大小是 {0}", sizeof(int));
         Console.WriteLine("short 的大小是 {0}", sizeof(short));
         Console.WriteLine("double 的大小是 {0}", sizeof(double));
         
         /* 三元运算符符的实例 */
         int a, b;
         a = 10;
         b = (a == 1) ? 20 : 30;
         Console.WriteLine("b 的值是 {0}", b);

         b = (a == 10) ? 20 : 30;
         Console.WriteLine("b 的值是 {0}", b);
         Console.ReadLine();
      }
   }
}

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

int 的大小是 4
short 的大小是 2
double 的大小是 8
b 的值是 30
b 的值是 20

The operator priority in C#

The operator's priority determines the combination of items in the expression. T his affects how an expression is evaluated. Some operators have a higher priority than others, for example, the multiplication operator has a higher priority than the plus-minus operator.

For example, x s 7 s 3 s 2, where x is assigned to 13 instead of 20, because the operator s has a higher priority than s, so the multiplication is calculated first, and then add 7.

The following table lists the operators by operator priority from highest to lowest, with higher priority operators appearing above the table and lower priority operators appearing below the table. In expressions, higher priority operators are evaluated first.

category Operator Conjunction
suffix () [] -> . ++ - - From left to right
One yuan + - ! ~ ++ - - (type)* & sizeof From right to left
Multiplication * / % From left to right
Decrease + - From left to right
Shift << >> From left to right
relation < <= > >= From left to right
equal == != From left to right
Bit and And & From left to right
Digital or xor ^ From left to right
Bit or OR | From left to right
Logic and AND && From left to right
Logic or OR || From left to right
condition ?: From right to left
Value = += -= *= /= %=>>= <<= &= ^= |= From right to left
comma , From left to right

Instance

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 / d 的值是 {0}", e);

         e = ((a + b) * c) / d;   // (30 * 15 ) / 5
         Console.WriteLine("((a + b) * c) / d 的值是 {0}", e);

         e = (a + b) * (c / d);   // (30) * (15/5)
         Console.WriteLine("(a + b) * (c / d) 的值是 {0}", e);

         e = a + (b * c) / d;    //  20 + (150/5)
         Console.WriteLine("a + (b * c) / d 的值是 {0}", e);
         Console.ReadLine();
      }
   }
}

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

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