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( = )和复合赋值( + = , -+ , * = , / = , %= ))用于将值或表达式的结果赋给左侧变量,以下是赋值运算符集,
"=" – it is used to assign value or an expression's result to the left side variable
“ =” –用于将值或表达式的结果分配给左侧变量
"+=" – 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)
"-=" – 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)
"/=" – 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)
"*=" – 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)
"%=" – 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# 赋值运算符