C# - 赋值运算符

C# 中的赋值运算符是什么?

C# 赋值运算符 在赋值的同时为变量赋值;在某些情况下,这些运算符执行数学运算。

C# 赋值运算符列表

C# 支持以下赋值运算符 -

运算符 符号 描述 示例
简单赋值 = 简单赋值运算符,将右侧操作数的值赋给左侧操作数 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 %一个
左移并赋值 <<= 左移与赋值运算符 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

赋值运算符示例

以下示例演示了 C# 中所有可用的赋值运算符 -

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();
      }
   }
}

当编译并执行上述代码时,它会产生以下结果 -

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

更多 C# 赋值运算符示例

示例 1:基本赋值 (=)

在此示例中,我们将一个值赋给一个变量并打印出来。

using System;

class Program
{
    static void Main()
    {
        int x = 10; // 将值 10 赋给 x
        Console.WriteLine("x: " + x);
    }
}

当编译并执行上述代码时,它会产生以下结果 -

x: 10

示例 2:使用 +=(加法和赋值)

在此示例中,我们使用 += 进行加法和赋值。

using System;

class Program
{
    static void Main()
    {
        int x = 5;
        x += 3; // 等价于 x = x + 3

        Console.WriteLine("x after += : " + x);
    }
}

当编译并执行上述代码时,它会产生以下结果 -

x after += : 8

示例 3:使用 -=(减法和赋值)

在此示例中,我们使用 -= 进行减法和赋值。

using System;

class Program
{
    static void Main()
    {
        int x = 10;
        x -= 4; // 等价于 x = x - 4

        Console.WriteLine("x after -= : " + x);
    }
}

当编译并执行上述代码时,它会产生以下结果 -

x after -= : 6

示例 4:使用 *=(乘法和赋值)

在此示例中,我们使用 *= 进行乘法和赋值。

using System;

class Program
{
    static void Main()
    {
        int x = 5;
        x *= 2; // 相当于 x = x * 2

        Console.WriteLine("x after *= : " + x);
    }
}

当编译并执行上述代码时,它会产生以下结果 -

x after *= : 10

示例 5:使用 /=(除法和赋值)

在此示例中,我们使用 /= 进行除法并赋值。

using System;

class Program
{
    static void Main()
    {
        int x = 20;
        x /= 4; // 相当于 x = x / 4

        Console.WriteLine("x after /= : " + x);
    }
}

当编译并执行上述代码时,它会产生以下结果 -

x after /= : 5

示例 6:使用 %=(取模和赋值)

在此示例中,我们使用取模并使用 %= 赋值。

using System;

class Program
{
    static void Main()
    {
        int x = 10;
        x %= 3; // 相当于 x = x % 3

        Console.WriteLine("x after %= : " + x);
    }
}

当编译并执行上述代码时,它会产生以下结果 -

x after %= : 1

赋值运算符的实际用例

示例 7:游戏中的累计分数

在此示例中,我们将分数添加到玩家的得分中。

using System;

class Program
{
    static void Main()
    {
        int score = 50;
        score += 10; // 添加点

        Console.WriteLine("Current Score: " + score);
    }
}

当编译并执行上述代码时,它会产生以下结果 -

Current Score: 60

示例 8:减少电商系统中的库存

在此示例中,当商品售出时,我们会减少库存。

using System;

class Program
{
    static void Main()
    {
        int stock = 100;
        stock -= 2; // 售出两件商品

        Console.WriteLine("Stock Remaining: " + stock);
    }
}

当编译并执行上述代码时,它会产生以下结果 -

Stock Remaining: 98

示例 9:投资金额翻倍

在此示例中,我们将投资金额翻倍。

using System;

class Program
{
    static void Main()
    {
        double investment = 1000;
        investment *= 2; // 加倍金额

        Console.WriteLine("Total Investment: " + investment);
    }
}

当编译并执行上述代码时,它会产生以下结果 -

Total Investment: 2000

最佳实践

以下是使用赋值运算符的最佳实践:

  • 计数器应使用 +=-=,而不是 x = x + 1
  • 使用 /= 之前,必须检查除数是否为零。
  • 应使用赋值运算符来保持代码简洁易读。
  • 在表达式中组合运算符时,必须了解运算符的优先级。