Latest web development tutorials

C # Operators

Operator is a symbol to tell the compiler to perform specific mathematical or logical operations. C # has a wealth of built-in operators, as follows:

  • Arithmetic operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Miscellaneous Operators

This tutorial will explain each of arithmetic operators, relational operators, logical operators, bitwise operators, assignment operator and other operators.

Arithmetic operators

The following table shows the C # support all arithmetic operators.A value of 10 is assumed variable, the variable Bis 20, then:

运算符描述实例
+把两个操作数相加 A + B 将得到 30
-从第一个操作数中减去第二个操作数 A - B 将得到 -10
*把两个操作数相乘 A * B 将得到 200
/分子除以分母 B / A 将得到 2
%取模运算符,整除后的余数 B % A 将得到 0
++自增运算符,整数值增加 1 A++ 将得到 11
--自减运算符,整数值减少 1 A-- 将得到 9

Examples

Consider the following examples to learn C # all the available arithmetic operators:

using System;

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

            c = a + b;
            Console.WriteLine ( "1 Line - c is {0}", c);
            c = a - b;
            Console.WriteLine ( "2 Line - c is {0}", c);
            c = a * b;
            Console.WriteLine ( "3 Line - c is {0}", c);
            c = a / b;
            Console.WriteLine ( "4 Line - c is {0}", c);
            c = a% b;
            Console.WriteLine ( "Line 5 - c is {0}", c);

            // ++ A first increment operation carried out by assignment c = ++ a;
            Console.WriteLine ( "6 Line - c is {0}", c);

            // In this case a value of 22
            // --a First self subtraction by assignment c = --a;
            Console.WriteLine ( "7 Line - c is {0}", c);
            Console.ReadLine ();
        }
    }
}

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

1 Line - the value of c is 31
2 Line - the value of c is 11
3 Line - is the value of c 210
4 Line - the value of c is 2
Line 5 - the value of c is 1
6 Line - the value of c is 22
7 Line - the value of c is 21
  • c = a ++: a first assignment to c, and then on a self-increment operator.
  • c = ++ a: first a self-increment operator, and then assigned to a c.
  • c = a--: a first assignment to c, and then on a self-subtraction.
  • c = --a: first a self-subtraction, then assigned to a c.
using System;

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

            // a++ 先赋值再进行自增运算
            b = a++;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();

            // ++a 先进行自增运算再赋值
            a = 1; // 重新初始化 a
            b = ++a;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();

            // a-- 先赋值再进行自减运算
            a = 1;  // 重新初始化 a
            b= a--;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();

            // --a 先进行自减运算再赋值
            a = 1;  // 重新初始化 a
            b= --a;
            Console.WriteLine("a = {0}", a);
            Console.WriteLine("b = {0}", b);
            Console.ReadLine();
        }
    }
}

Running instance »

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 the C # support all the relational operators.A value of 10 is assumed variable, the variable Bis 20, then:

运算符描述实例
==检查两个操作数的值是否相等,如果相等则条件为真。 (A == B) 不为真。
!=检查两个操作数的值是否相等,如果不相等则条件为真。 (A != B) 为真。
>检查左操作数的值是否大于右操作数的值,如果是则条件为真。 (A > B) 不为真。
<检查左操作数的值是否小于右操作数的值,如果是则条件为真。 (A < B) 为真。
>=检查左操作数的值是否大于或等于右操作数的值,如果是则条件为真。 (A >= B) 不为真。
<=检查左操作数的值是否小于或等于右操作数的值,如果是则条件为真。 (A <= B) 为真。

Examples

Consider the following examples to learn C # all the available relational operators:

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 not equal to b");
      }
      if (a <b)
      {
          Console.WriteLine ( "Line 2 - a less than b");
      }
      else
      {
          Console.WriteLine ( "Line 2 - a not less than b");
      }
      if (a> b)
      {
          Console.WriteLine ( "Line 3 - a is greater than b");
      }
      else
      {
          Console.WriteLine ( "Line 3 - a not greater than b");
      }
      / * Change the value of a and b * /
      a = 5;
      b = 20;
      if (a <= b)
      {
         Console.WriteLine ( "Line 4 - a 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 produces the following results:

Line 1 - a not equal to b
Line 2 - a 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 the C # support all logical operators. Suppose the variableA Boolean value true, the variable Bis a Boolean value false, then:

运算符描述实例
&&称为逻辑与运算符。如果两个操作数都非零,则条件为真。 (A && B) 为假。
||称为逻辑或运算符。如果两个操作数中有任意一个非零,则条件为真。 (A || B) 为真。
!称为逻辑非运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。 !(A && B) 为真。

Examples

Consider the following examples to learn C # all the available logical operators:

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 value 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 produces the following results:

Line 1 - the condition is true Line 2 - the condition is true Line 3 - the condition is not true Line 4 - condition is true 

Bitwise Operators

Bitwise operators acting on the bit, and bit by bit operation. &, | And ^ truth table is as follows:

pqp & qp | qp ^ q
00000
01011
11110
10011

Suppose if A = 60, and B = 13, and now, in binary form, 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 C # support bitwise operators. Suppose the variableA is 60, the value of the variable B13, then:

运算符描述实例
&如果同时存在于两个操作数中,二进制 AND 运算符复制一位到结果中。 (A & B) 将得到 12,即为 0000 1100
|如果存在于任一操作数中,二进制 OR 运算符复制一位到结果中。 (A | B) 将得到 61,即为 0011 1101
^如果存在于其中一个操作数中但不同时存在于两个操作数中,二进制异或运算符复制一位到结果中。 (A ^ B) 将得到 49,即为 0011 0001
~二进制补码运算符是一元运算符,具有"翻转"位效果。(~A ) 将得到 -61,即为 1100 0011,2 的补码形式,带符号的二进制数。
<<二进制左移运算符。左操作数的值向左移动右操作数指定的位数。 A << 2 将得到 240,即为 1111 0000
>>二进制右移运算符。左操作数的值向右移动右操作数指定的位数。 A >> 2 将得到 15,即为 0000 1111

Examples

Consider the following examples to learn C #, all available bits operators:

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 ( "1 Line - c is {0}", c);

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

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

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

             c = a << 2; / * 240 = 1111 0000 * /
             Console.WriteLine ( "Line 5 - c is {0}", c);

             c = a >> 2; / * 15 = 0000 1111 * /
             Console.WriteLine ( "6 Line - c is {0}", c);
            Console.ReadLine ();
        }
    }
}

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

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

Assignment Operators

The following table lists the C # support the assignment operator:

运算符描述实例
=简单的赋值运算符,把右边操作数的值赋给左边操作数 C = A + B 将把 A + B 的值赋给 C
+=加且赋值运算符,把右边操作数加上左边操作数的结果赋值给左边操作数 C += A 相当于 C = C + A
-=减且赋值运算符,把左边操作数减去右边操作数的结果赋值给左边操作数 C -= A 相当于 C = C - A
*=乘且赋值运算符,把右边操作数乘以左边操作数的结果赋值给左边操作数 C *= A 相当于 C = C * A
/=除且赋值运算符,把左边操作数除以右边操作数的结果赋值给左边操作数 C /= A 相当于 C = C / A
%=求模且赋值运算符,求两个操作数的模赋值给左边操作数 C %= A 相当于 C = C % A
<<=左移且赋值运算符 C <<= 2 等同于 C = C << 2
>>=右移且赋值运算符 C >>= 2 等同于 C = C >> 2
&=按位与且赋值运算符 C &= 2 等同于 C = C & 2
^=按位异或且赋值运算符 C ^= 2 等同于 C = C ^ 2
|=按位或且赋值运算符 C |= 2 等同于 C = C | 2

Examples

Consider the following examples to learn C # in all available assignment operators:

using System;

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

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

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

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

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

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

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

            c << = 2;
            Console.WriteLine ( "Line 7 - << = c value = {0}", c);

            c >> = 2;
            Console.WriteLine ( "Line 8 - >> = c value = {0}", c);

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

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

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

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

Line 1 - = c = value 21
Line 2 - + = value of c = 42
3 Line - - = the value of c = 21
4 Line - * = value of c = 441
Line 5 - / = c = value 21
6 Line -% = the value of c = 11
7 Line - << = value of c = 44
8 Line - the value >> = c = 11
9 Line - & = value of c = 2
Line 10 - ^ = c = 0 value
Line 11 - | = c 2 = value

Miscellaneous Operators

The following table lists the C # support some other important operators, includingsizeof, typeofand?:.

Operators description Examples sizeof () Return data type size. sizeof (int), will return 4. typeof () Returns the class type. typeof (StreamReader); & Return address variable. & A; you will get the actual address of the variable. * Pointer variable. * A; will point to a variable. ?: Conditional expression ? If the condition is true, or X: Y otherwise is Determines whether an object of a certain type. If (Ford is Car) // check whether an object Ford Car class. as Cast, even if the conversion fails it will not throw an exception. Object obj = new StringReader ( "Hello");
StringReader r = obj as StringReader;

Examples

using System;

namespace OperatorsAppl
{
    
   class Program
   {
      static void Main (string [] args)
      {
         
         / * Sizeof operator instance * /
         Console.WriteLine ( "int size is {0}", sizeof (int));
         Console.WriteLine ( "short size is {0}", sizeof (short));
         Console.WriteLine ( "double the size of the {0}", sizeof (double));
         
         / * Ternary operator breaks instance * /
         int a, b;
         a = 10;
         b = (a == 1) 20: 30;?
         Console.WriteLine ( "b values ​​are {0}", b);

         b = (a == 10) 20: 30;?
         Console.WriteLine ( "b values ​​are {0}", b);
         Console.ReadLine ();
      }
   }
}

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

Int size is 4
short size is 2
double size is 8
b values ​​are 30
b values ​​are 20

In C # operator precedence

Operator precedence determine a combined term of an expression. This affects how an expression calculation. Some operators have a higher priority than other operators, such as addition and subtraction, multiplication and division operator than the operator a higher priority.

For example, x = 7 + 3 * 2, where, x is assigned to 13 instead of 20, because the * operator has higher precedence than +, so first calculate the multiplication 3 * 2, then add 7.

The following table will be listed in order of operator precedence for each operator, having the above higher priority operators appear in the table, with the following lower priority operators appear in the table. In the expression, higher priority operators priority is calculated.

类别  运算符 结合性 
后缀 () [] -> . ++ - -   从左到右 
一元  + - ! ~ ++ - - (type)* & sizeof  从右到左 
乘除  * / % 从左到右 
加减 + -  从左到右 
移位  << >>  从左到右 
关系 < <= > >=  从左到右 
相等  == !=  从左到右 
位与 AND  从左到右 
位异或 XOR  从左到右 
位或 OR  从左到右 
逻辑与 AND &&  从左到右 
逻辑或 OR  ||  从左到右 
条件 ?:  从右到左 
赋值  = += -= *= /= %=>>= <<= &= ^= |= 从右到左 
逗号  从左到右 

Examples

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 ( "value (a + b) * c / d is {0}", e);

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

         e = (a + b) * (c / d); // (30) * (15/5)
         Console.WriteLine ( "(a + b) * (c / d) values ​​are {0}", e);

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

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

Value (a + b) * c / d is 90
Value ((a + b) * c) / d is 90
(A + b) * (c / d) value is 90
The value of a + (b * c) / d is 50