c# 赋值运算符_C#程序演示赋值运算符的示例

c# 赋值运算符

Assignment operators (Assignment (=) and compound assignments (+=, -+, *=, /=, %=)) are used to assign the value or an expression's result to the left side variable, following are the set of assignment operators,

赋值运算符(Assignment( = )和复合赋值( + = , -+ , * = , / = , %= ))用于将值或表达式的结果赋给左侧变量,以下是赋​​值运算符集,

  1. "=" – it is used to assign value or an expression's result to the left side variable

    “ =” –用于将值或表达式的结果分配给左侧变量

  2. "+=" – it is used to add second operand to the existing operand's value and assigns it back (a+=b is equal to a=a+b)

    “ + =” –用于将第二个操作数添加到现有操作数的值并将其赋值回去(a + = b等于a = a + b)

  3. "-=" – it is used to subtract second operand from the existing operand's value and assigns it back (a-=b is equal to a=a-b)

    “-=” –用于从现有操作数的值中减去第二个操作数并将其赋值回去(a- = b等于a = ab)

  4. "/=" – it is used to divide second operand from the existing operand's value and assigns it back (a/=b is equal to a=a+b)

    “ / =” –用于将第二个操作数与现有操作数的值相除并将其赋值回去(a / = b等于a = a + b)

  5. "*=" – it is used to multiply second operand with the existing operand's value and assigns it back (a*=b is equal to a=a*b)

    “ * =” –用于将第二个操作数与现有操作数的值相乘并赋值回去(a * = b等于a = a * b)

  6. "%=" – it is used to get the remainder by dividing second operand with the existing operand's value and assigns it back (a%=b is equal to a=a%b)

    “%=” –用于通过将第二个操作数除以现有操作数的值来获得余数,并将其赋值回去(a%= b等于a = a%b)

Example:

例:

    Input:
    int a = 10;
    int b = 3;

    //operations & outputs
    a = 100;    //value of a will be 100
    a += b;     //value of a will be 103
    a -= b;     //value of a will be 100
    a *= b;     //value of a will be 300
    a /= b;     //value of a will be 100
    a %= b;     //value of a will be 1

C# code to demonstrate example of assignment operators

C#代码演示赋值运算符的示例

// C# program to demonstrate example of 
// assignment operators 
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            int a = 10;
            int b = 3;

            Console.WriteLine("a: {0}", a);
            a = 100; //assigment
            Console.WriteLine("a: {0}", a);
            a += b;
            Console.WriteLine("a: {0}", a);
            a -= b;
            Console.WriteLine("a: {0}", a);
            a *= b;
            Console.WriteLine("a: {0}", a);
            a /= b;
            Console.WriteLine("a: {0}", a);
            a %= b;
            Console.WriteLine("a: {0}", a);


            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

输出量

a: 10
a: 100
a: 103
a: 100
a: 300
a: 100
a: 1


翻译自: https://www.includehelp.com/dot-net/assignment-operators-example-in-c-sharp.aspx

c# 赋值运算符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值