📜  C#|运营商

📅  最后修改于: 2021-05-29 18:22:11             🧑  作者: Mango

运算符是任何编程语言的基础。因此,如果不使用运算符,则C#语言的功能是不完整的。运算符允许我们对操作数执行不同类型的运算。在C#中,可以根据运算符的不同功能对其进行分类:

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 按位运算符
  • 赋值运算符
  • 条件运算符

在C#中,运算符还可以根据操作数的数量进行分类

  • 一元运算符:采用一个操作数执行运算的运算符。
  • 二进制运算符:需要两个操作数来执行运算的运算符。
  • 三元运算符:需要三个操作数来执行运算的运算符。

算术运算符

这些用于对操作数执行算术/数学运算。属于此类的二元运算符是:

  • 加法: ‘+’运算符将两个操作数相加。例如, x + y
  • 减法: ‘-‘运算符减去两个操作数。例如, xy
  • 乘法: ‘*’运算符将两个操作数相乘。例如, x * y
  • 除法: ‘/’运算符将第一个操作数除以第二个操作数。例如, x / y
  • 模量:当第一个操作数除以第二个操作数时, ‘%’运算符返回余数。例如, x%y

例子:

// C# program to demonstrate the working 
// of Binary Arithmetic Operators
using System;
namespace Arithmetic
{
    class GFG
    {
          
        // Main Function
        static void Main(string[] args)
        {
              
            int result;
            int x = 10, y = 5;
              
            // Addition
            result = (x + y);
            Console.WriteLine("Addition Operator: " + result);
              
            // Subtraction
            result = (x - y);
            Console.WriteLine("Subtraction Operator: " + result);
              
            // Multiplication
            result = (x * y);
            Console.WriteLine("Multiplication Operator: "+ result);
              
            // Division
            result = (x / y);
            Console.WriteLine("Division Operator: " + result);
              
            // Modulo
            result = (x % y);
            Console.WriteLine("Modulo Operator: " + result);
        }
    }
}

输出 :

Addition Operator: 15
Subtraction Operator: 5
Multiplication Operator: 50
Division Operator: 2
Modulo Operator: 0

属于一元运算符类别的是:

  • 递增: ‘++’运算符用于递增整数的值。当放置在变量名之前(也称为pre-increment 运算符),其值立即增加。例如, ++ x
    并且当将其放在变量名之后(也称为post-increment 运算符 )时,其值将暂时保留,直到执行此语句为止,并且在下一个语句执行之前对其进行更新。例如, x ++
  • 递减: “ –”运算符用于递减整数值。当放置在变量名(也称为pre-decrement 运算符 )之前时,其值立即减小。例如, –x
    并且当将其放在变量名之后(也称为post-decrement运算符)时,其值将暂时保留,直到执行该语句为止,并且在下一个语句执行之前对其进行更新。例如, x–

    例子 :

    // C# program to demonstrate the working 
    // of Unary Arithmetic Operators
    using System;
    namespace Arithmetic {
          
        class GFG {
              
            // Main Function
            static void Main(string[] args)
            {
                  
                int a = 10, res;
       
                // post-increment example:
                // res is assigned 10 only, 
                // a is not updated yet
                res = a++;
                  
                 //a becomes 11 now
                Console.WriteLine("a is {0} and res is {1}", a, res);
               
               
                // post-decrement example:
                // res is assigned 11 only, a is not updated yet
                res = a--;
                  
                //a becomes 10 now
                Console.WriteLine("a is {0} and res is {1}", a, res);  
               
               
                // pre-increment example:
                // res is assigned 11 now since a
                // is updated here itself
                res = ++a;
                  
                // a and res have same values = 11
                Console.WriteLine("a is {0} and res is {1}", a, res);
               
               
                // pre-decrement example:
                // res is assigned 10 only since
                // a is updated here itself
                res = --a;
                  
                // a and res have same values = 10
                Console.WriteLine("a is {0} and res is {1}",a, res); 
               
                 
            }
        }
    }
    

    输出 :

    a is 11 and res is 10
    a is 10 and res is 11
    a is 11 and res is 11
    a is 10 and res is 10
    

关系运算符

关系运算符用于比较两个值。让我们一一看一下:

  • ‘==’(等于)运算符检查两个给定的操作数是否相等。如果是这样,则返回true。否则,它返回false。例如, 5 == 5将返回true。
  • ‘!=’(不等于)运算符检查两个给定的操作数是否相等。如果不是,则返回true。否则,它返回false。它是‘==’运算符的精确布尔补码。例如, 5!= 5将返回false。
  • ‘>’(大于)运算符检查第一个操作数是否大于第二个操作数。如果是这样,则返回true。否则,它返回false。例如, 6> 5将返回true。
  • “ <”(小于)运算符检查第一个操作数是否小于第二个操作数。如果是这样,则返回true。否则,它返回false。例如, 6 <5将返回false。
  • ‘> =’(大于等于)运算符检查第一个操作数是否大于或等于第二个操作数。如果是这样,则返回true。否则,它返回false。例如, 5> = 5将返回true。
  • “ <=”(小于等于)运算符检查第一个操作数是否小于或等于第二个操作数。如果是这样,则返回true。否则,它返回false。例如, 5 <= 5也将返回true。

例子 :

// C# program to demonstrate the working 
// of Relational Operators
using System;
namespace Relational {
      
class GFG {
      
    // Main Function
    static void Main(string[] args)
    {
        bool result;
        int x = 5, y = 10;
          
        // Equal to Operator
        result = (x == y);
        Console.WriteLine("Equal to Operator: " + result);
          
        // Greater than Operator
        result = (x > y);
        Console.WriteLine("Greater than Operator: " + result);
          
        // Less than Operator
        result = (x < y);
        Console.WriteLine("Less than Operator: " + result);
          
        // Greater than Equal to Operator
        result = (x >= y);
        Console.WriteLine("Greater than or Equal to: "+ result);
          
        // Less than Equal to Operator
        result = (x <= y);
        Console.WriteLine("Lesser than or Equal to: "+ result);
          
        // Not Equal To Operator
        result = (x != y);
        Console.WriteLine("Not Equal to Operator: " + result);
    }
}
}

输出 :

Equal to Operator: False
Greater than Operator: False
Less than Operator: True
Greater than or Equal to: False
Lesser than or Equal to: True
Not Equal to Operator: True

逻辑运算符

它们用于组合两个或多个条件/约束或补充所考虑的原始条件的评估。如下所述:

  • 逻辑AND:当同时考虑到两个条件时, “ &&”运算符返回true。否则,它返回false。例如,当a和b都为真(即非零)时, && b返回真。
  • 逻辑或: “ ||”当满足所考虑的条件中的一个(或两个)时,运算符将返回true。否则,它返回false。例如, ||如果a或b之一为true(即非零),则b返回true。当然,当a和b都为true时,它将返回true。
  • 逻辑非: “!”运算符返回true,即不满足考虑条件。否则,它返回false。例如,如果a为假,即a = 0时, !a返回true。

例子 :

// C# program to demonstrate the working 
// of Logical Operators
using System;
namespace Logical {
      
class GFG {
      
    // Main Function
    static void Main(string[] args)
    {
            bool a = true,b = false, result;
          
            // AND operator
            result = a && b;
            Console.WriteLine("AND Operator: " + result);
              
            // OR operator
            result = a || b;
            Console.WriteLine("OR Operator: " + result);
              
            // NOT operator
            result = !a;
            Console.WriteLine("NOT Operator: " + result);
          
    }
}
}

输出 :

AND Operator: False
OR Operator: True
NOT Operator: False

按位运算符

在C#中,有6个按位运算运算符可按位工作或用于逐位操作。以下是按位运算运算符:

  • &(按位与)将两个数字作为操作数,并对两个数字的每一位进行“与”运算。仅当两个位均为1时,AND的结果才为1。
  • | (按位或)将两个数字作为操作数,并对两个数字的每一位进行“或”运算。 OR的结果为1,两个位中的任何一个为1。
  • ^(按位XOR)将两个数字用作操作数,并对两个数字的每一位进行XOR。如果两个位不同,则XOR的结果为1。
  • <<(左移)取两个数字,左移第一个操作数的位,第二个操作数确定要移位的位数。
  • >>(右移)取两个数字,右移第一个操作数的位,第二个操作数确定要移位的位数。

例子 :

// C# program to demonstrate the working 
// of Bitwise Operators
using System;
namespace Bitwise {
      
class GFG {
      
    // Main Function
    static void Main(string[] args)
    {
         int x = 5, y = 10, result;
           
            // Bitwise AND Operator
            result = x & y;
            Console.WriteLine("Bitwise AND: " + result);
              
            // Bitwise OR Operator
            result = x | y;
            Console.WriteLine("Bitwise OR: " + result);
              
            // Bitwise XOR Operator
            result = x ^ y;
            Console.WriteLine("Bitwise XOR: " + result);
              
            // Bitwise AND Operator
            result = ~x;
            Console.WriteLine("Bitwise Complement: " + result);
              
            // Bitwise LEFT SHIFT Operator
            result = x << 2;
            Console.WriteLine("Bitwise Left Shift: " + result);
              
            // Bitwise RIGHT SHIFT Operator
            result = x >> 2;
            Console.WriteLine("Bitwise Right Shift: " + result);
          
    }
}
}

输出 :

Bitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise Complement: -6
Bitwise Left Shift: 20
Bitwise Right Shift: 1

赋值运算符

赋值运算符用于为变量赋值。赋值运算符的左侧操作数是一个变量,而赋值运算符的右侧操作数是一个值。右侧的值必须与左侧的变量具有相同的数据类型,否则编译器将引发错误。
不同类型的赋值运算符如下所示:

  • “ =”(简单分配) :这是最简单的赋值运算符。该运算符用于将右侧的值分配给左侧的变量。
    例子 :
    a = 10;
    b = 20;
    ch = 'y';
    
  • “ + =”(添加分配) :此运算符是’+’和’=’运算符。该运算符首先将左侧变量的当前值添加到右侧的值,然后将结果分配给左侧的变量。
    例子 :
    (a += b) can be written as (a = a + b)
    

    如果存储在a中的初始值是5。那么(a + = 6)= 11。

  • “-=”(减法赋值) :此运算符是’-‘和’=’运算符。该运算符首先从右侧的值中减去左侧变量的当前值,然后将结果分配给左侧的变量。
    例子 :
    (a -= b) can be written as (a = a - b)
    

    如果存储在a中的初始值是8,则(a-= 6)= 2。

  • “ * =”(乘法分配) :此运算符是’*’和’=’运算符。该运算符首先将左侧变量的当前值乘以右侧值,然后将结果分配给左侧变量。
    例子 :
    (a *= b) can be written as (a = a * b)
    

    如果最初存储在a中的值为5。则(a * = 6)= 30。

  • “ / =”(部门分配) :此运算符是’/’和’=’运算符。该运算符首先将左侧变量的当前值除以右侧值,然后将结果分配给左侧变量。
    例子 :
    (a /= b) can be written as (a = a / b)
    

    如果存储在a中的初始值是6。则(a / = 2)= 3。

  • “%=”(模分配) :此运算符是’%’和’=’运算符。该运算符首先对左侧变量的当前值乘以右侧变量的值,然后将结果赋给左侧变量。
    例子 :
    (a %= b) can be written as (a = a % b)
    

    如果最初存储在a中的值为6。则(a%= 2)= 0。

  • “ << =“(左移位分配) :此运算符是'<(a << = 2)的组合,可以写为(a = a << << 2)

    如果最初存储在a中的值为6。则(a << = 2)= 24。

  • “ >> =”(右移分配) :此运算符是’>>’和’=’运算符。该运算符首先向右移变量的当前值,向右移左值,然后将结果分配给左变量。
    例子 :
    (a >>= 2) can be written as (a = a >> 2)
    

    如果存储在a中的初始值是6,则(a >> = 2)= 1。

  • “&=”(按位与分配) :此运算符是’&’和’=’运算符。该运算符首先将左边变量的当前值与右边的值“按位与”,然后将结果分配给左边的变量。
    例子 :
    (a &= 2) can be written as (a = a & 2)
    

    如果存储在a中的初始值是6,则(a&= 2)= 2。

  • “ ^ =”(按位异或) :此运算符是’^’和’=’运算符。该运算符首先将左侧变量的当前值与右侧变量进行“按位异或”,然后将结果分配给左侧变量。
    例子 :
    (a ^= 2) can be written as (a = a ^ 2)
    

    如果存储在a中的初始值是6,则(a ^ = 2)= 4。

  • “ | =”(按位或) :此运算符是“ |”的组合和’=’运算符。该运算符首先将左边变量的当前值与右边的值“按位或”,然后将结果分配给左边的变量。
    例子 :
    (a |= 2) can be written as (a = a | 2)
    

    如果存储在a中的初始值是6。则(a | = 2)= 6。

例子 :

// C# program to demonstrate the working 
// of Assignment Operators
using System;
namespace Assignment {
      
class GFG {
      
    // Main Function
    static void Main(string[] args)
    {
          
            // initalize variable x
            // using Simple Assignment 
            // Operator "="
            int x = 15; 
              
            // it means x = x + 10
            x += 10; 
            Console.WriteLine("Add Assignment Operator: " + x);
              
             // initalize variable x again
            x = 20;
              
            // it means x = x - 5
            x -= 5; 
            Console.WriteLine("Subtract Assignment Operator: " + x);
              
            // initalize variable x again
            x = 15;
              
            // it means x = x * 5
            x *= 5; 
            Console.WriteLine("Multiply Assignment Operator: " + x);
              
            // initalize variable x again
            x = 25;
              
            // it means x = x / 5
            x /= 5; 
            Console.WriteLine("Division Assignment Operator: " + x);
              
            // initalize variable x again
            x = 25;
              
            // it means x = x % 5
            x %= 5; 
            Console.WriteLine("Modulo Assignment Operator: " + x);
              
            // initalize variable x again
            x = 8;
              
            // it means x = x << 2
            x <<= 2; 
            Console.WriteLine("Left Shift Assignment Operator: " + x);
              
            // initalize variable x again
            x = 8;
              
            // it means x = x >> 2
            x >>= 2; 
            Console.WriteLine("Right Shift Assignment Operator: " + x);
              
            // initalize variable x again
            x = 12;
              
            // it means x = x >> 4
            x &= 4; 
            Console.WriteLine("Bitwise AND Assignment Operator: " + x);
              
            // initalize variable x again
            x = 12;
              
            // it means x = x >> 4
            x ^= 4; 
            Console.WriteLine("Bitwise Exclusive OR Assignment Operator: " + x);
              
             // initalize variable x again
            x = 12;
              
            // it means x = x >> 4
            x |= 4; 
            Console.WriteLine("Bitwise Inclusive OR Assignment Operator: " + x);
          
    }
}
}

输出 :

Add Assignment Operator: 25
Subtract Assignment Operator: 15
Multiply Assignment Operator: 75
Division Assignment Operator: 5
Modulo Assignment Operator: 0
Left Shift Assignment Operator: 32
Right Shift Assignment Operator: 2
Bitwise AND Assignment Operator: 4
Bitwise Exclusive OR Assignment Operator: 8
Bitwise Inclusive OR Assignment Operator: 12

条件运算符

它是三元运算符,它是if-else语句的简写形式。它具有三个操作数,因此名称为三元。它将返回两个值之一,具体取决于布尔表达式的值。

句法 :

condition ? first_expression : second_expression;

解释 :
条件:必须评估为true或false。
如果条件为真
将对first_expression求值并成为结果。
如果条件为假,
将对second_expression进行评估并成为结果。

例子 :

// C# program to demonstrate the working 
// of Conditional Operator
using System;
namespace Conditional {
      
class GFG {
      
    // Main Function
    static void Main(string[] args)
    {
            int x = 5, y = 10, result;
              
            // To find which value is greater
            // Using Conditional Operator
            result = x > y ? x : y; 
              
            // To display the result 
            Console.WriteLine("Result: " + result);
              
            // To find which value is greater
            // Using Conditional Operator
            result = x < y ? x : y; 
              
            // To display the result
            Console.WriteLine("Result: " + result);
    }
}
}

输出 :

Result: 10
Result: 5