📜  C＃运算符

📅  最后修改于: 2020-10-06 09:35:20             🧑  作者: Mango

##### 1.基本分配运算符

``````double x;
x = 50.05;``````

### 示例1：基本分配运算符

``````using System;

namespace Operator
{
class AssignmentOperator
{
public static void Main(string[] args)
{
int firstNumber, secondNumber;
// Assigning a constant to variable
firstNumber = 10;
Console.WriteLine("First Number = {0}", firstNumber);

// Assigning a variable to another variable
secondNumber = firstNumber;
Console.WriteLine("Second Number = {0}", secondNumber);
}
}
}
``````

``````First Number = 10
Second Number = 10``````

##### 2.算术运算符

``````int x = 5;
int y = 10;
int z = x + y;// z = 15
``````
C# Arithmetic Operators
Operator Operator Name Example
+ Addition Operator 6 + 3 evaluates to 9
Subtraction Operator 10 – 6 evaluates to 4
* Multiplication Operator 4 * 2 evaluates to 8
/ Division Operator 10 / 5 evaluates to 2
% Modulo Operator (Remainder) 16 % 3 evaluates to 1

### 示例2：算术运算符

``````using System;

namespace Operator
{
class ArithmeticOperator
{
public static void Main(string[] args)
{
double firstNumber = 14.40, secondNumber = 4.60, result;
int num1 = 26, num2 = 4, rem;

result = firstNumber + secondNumber;
Console.WriteLine("{0} + {1} = {2}", firstNumber, secondNumber, result);

// Subtraction operator
result = firstNumber - secondNumber;
Console.WriteLine("{0} - {1} = {2}", firstNumber, secondNumber, result);

// Multiplication operator
result = firstNumber * secondNumber;
Console.WriteLine("{0} * {1} = {2}", firstNumber, secondNumber, result);

// Division operator
result = firstNumber / secondNumber;
Console.WriteLine("{0} / {1} = {2}", firstNumber, secondNumber, result);

// Modulo operator
rem = num1 % num2;
Console.WriteLine("{0} % {1} = {2}", num1, num2, rem);
}
}
}
``````

``````14.4 + 4.6 = 19
14.4 - 4.6 = 9.8
14.4 * 4.6 = 66.24
14.4 / 4.6 = 3.1304347826087
26 % 4 = 2``````

``````result = 4.5 + 2.7 ; // result will hold 7.2
result = firstNumber - 3.2; // result will hold 11.2``````

##### 3.关系运算符

C# Relational Operators
Operator Operator Name Example
== Equal to 6 == 4 evaluates to false
> Greater than 3 > -1 evaluates to true
< Less than 5 < 3 evaluates to false
>= Greater than or equal to 4 >= 4 evaluates to true
<= Less than or equal to 5 <= 3 evaluates to false
!= Not equal to 10 != 2 evaluates to true

### 示例3：关系运算符

``````using System;

namespace Operator
{
class RelationalOperator
{
public static void Main(string[] args)
{
bool result;
int firstNumber = 10, secondNumber = 20;

result = (firstNumber==secondNumber);
Console.WriteLine("{0} == {1} returns {2}",firstNumber, secondNumber, result);

result = (firstNumber > secondNumber);
Console.WriteLine("{0} > {1} returns {2}",firstNumber, secondNumber, result);

result = (firstNumber < secondNumber);
Console.WriteLine("{0} < {1} returns {2}",firstNumber, secondNumber, result);

result = (firstNumber >= secondNumber);
Console.WriteLine("{0} >= {1} returns {2}",firstNumber, secondNumber, result);

result = (firstNumber <= secondNumber);
Console.WriteLine("{0} <= {1} returns {2}",firstNumber, secondNumber, result);

result = (firstNumber != secondNumber);
Console.WriteLine("{0} != {1} returns {2}",firstNumber, secondNumber, result);
}
}
}
``````

``````10 == 20 returns False
10 > 20 returns False
10 < 20 returns True
10 >= 20 returns False
10 <= 20 returns True
10 != 20 returns True``````

##### 4.逻辑运算符

C# Logical operators
Operand 1 Operand 2 OR (||) AND (&&)
true true true true
true false true false
false true true false
false false false false

• 如果操作数之一为true，则`OR` 运算符会将其评估为`true`
• 如果操作数之一为false，则`AND` 运算符会将其评估为`false`

### 示例4：逻辑运算符

``````using System;

namespace Operator
{
class LogicalOperator
{
public static void Main(string[] args)
{
bool result;
int firstNumber = 10, secondNumber = 20;

// OR operator
result = (firstNumber == secondNumber) || (firstNumber > 5);
Console.WriteLine(result);

// AND operator
result = (firstNumber == secondNumber) && (firstNumber > 5);
Console.WriteLine(result);
}
}
}
``````

``````True
False``````

##### 5.一元运算符

C# unary operators
Operator Operator Name Description
+ Unary Plus Leaves the sign of operand as it is
Unary Minus Inverts the sign of operand
++ Increment Increment value by 1
Decrement Decrement value by 1
! Logical Negation (Not) Inverts the value of a boolean

### 示例5：一元运算符

``````using System;

namespace Operator
{
class UnaryOperator
{
public static void Main(string[] args)
{
int number = 10, result;
bool flag = true;

result = +number;
Console.WriteLine("+number = " + result);

result = -number;
Console.WriteLine("-number = " + result);

result = ++number;
Console.WriteLine("++number = " + result);

result = --number;
Console.WriteLine("--number = " + result);

Console.WriteLine("!flag = " + (!flag));
}
}
}
``````

``````+number = 10
-number = -10
++number = 11
--number = 10
!flag = False``````

### 示例6：C＃中的Post and Pre Increment 运算符

``````using System;

namespace Operator
{
class UnaryOperator
{
public static void Main(string[] args)
{
int number = 10;

Console.WriteLine((number++));
Console.WriteLine((number));

Console.WriteLine((++number));
Console.WriteLine((number));
}
}
}
``````

``````10
11
12
12``````

``Console.WriteLine((number++));``

``Console.WriteLine((++number));``

##### 6.三元运算符

``variable = Condition? Expression1 : Expression2;``

• 如果Condition表示的表达式为`true` ，则将Expression1的结果分配给变量。
• 如果为`false` ，则将Expression2的结果分配给变量。

### 示例7：三元运算符

``````using System;

namespace Operator
{
class TernaryOperator
{
public static void Main(string[] args)
{
int number = 10;
string result;

result = (number % 2 == 0)? "Even Number" : "Odd Number";
Console.WriteLine("{0} is {1}", number, result);
}
}
}
``````

``10 is Even Number``

##### 7.按位和移位运算符

C# Bitwise and Bit Shift operators
Operator Operator Name
~ Bitwise Complement
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
<< Bitwise Left Shift
>> Bitwise Right Shift

### 例8：按位和移位操作

``````using System;

namespace Operator
{
class BitOperator
{
public static void Main(string[] args)
{
int firstNumber = 10;
int secondNumber = 20;
int result;

result = ~firstNumber;
Console.WriteLine("~{0} = {1}", firstNumber, result);

result = firstNumber & secondNumber;
Console.WriteLine("{0} & {1} = {2}", firstNumber,secondNumber, result);

result = firstNumber | secondNumber;
Console.WriteLine("{0} | {1} = {2}", firstNumber,secondNumber, result);

result = firstNumber ^ secondNumber;
Console.WriteLine("{0} ^ {1} = {2}", firstNumber,secondNumber, result);

result = firstNumber << 2;
Console.WriteLine("{0} << 2 = {1}", firstNumber, result);

result = firstNumber >> 2;
Console.WriteLine("{0} >> 2 = {1}", firstNumber, result);
}
}
}
``````

``````~10 = -11
10 & 20 = 0
10 | 20 = 30
10 ^ 20 = 30
10 << 2 = 40
10 >> 2 = 2``````

##### 8.复合赋值运算符
C# Compound Assignment Operators
Operator Operator Name Example Equivalent To
+= Addition Assignment `x += 5` `x = x + 5`
-= Subtraction Assignment `x -= 5` `x = x - 5`
*= Multiplication Assignment `x *= 5` `x = x * 5`
/= Division Assignment `x /= 5` `x = x / 5`
%= Modulo Assignment `x %= 5` `x = x % 5`
&= Bitwise AND Assignment `x &= 5` `x = x & 5`
|= Bitwise OR Assignment `x |= 5` `x = x | 5`
^= Bitwise XOR Assignment `x ^= 5` `x = x ^ 5`
<<= Left Shift Assignment `x <<= 5` `x = x << 5`
>>= Right Shift Assignment `x >>= 5` `x = x >> 5`
=> Lambda Operator `x => x*x` `Returns x*x`

### 示例9：复合赋值运算符

``````using System;

namespace Operator
{
class BitOperator
{
public static void Main(string[] args)
{
int number = 10;

number += 5;
Console.WriteLine(number);

number -= 3;
Console.WriteLine(number);

number *= 2;
Console.WriteLine(number);

number /= 3;
Console.WriteLine(number);

number %= 3;
Console.WriteLine(number);

number &= 10;
Console.WriteLine(number);

number |= 14;
Console.WriteLine(number);

number ^= 12;
Console.WriteLine(number);

number <<= 2;
Console.WriteLine(number);

number >>= 3;
Console.WriteLine(number);
}
}
}
``````

``````15
12
24
8
2
2
14
2
8
1``````