运算符是一个符号,它告诉编译器执行特定的数学或逻辑操作,C#具有丰富的内置运算符集,并提供以下类型的运算符-
下表显示了C#支持的所有算术运算符。假设变量A=10,变量B=20,则-
| Operator | Remark | Example |
|---|---|---|
| + | 相加 | A + B=30 |
| - | 相减 | A- B=-10 |
| * | 相乘 | A * B=200 |
| / | 相除 | B/A=2 |
| % | 取模 | B % A=0 |
| ++ | 自增+1 | A++=11 |
| -- | 自减-1 | A--=9 |
以下示例演示了C#-中可用的所有算术运算符
using System;
namespace OperatorsAppl {
class Program {
static void Main(string[] args) {
int a=21;
int b=10;
int c;
c=a + b;
Console.WriteLine("Line 1- Value of c is {0}", c);
c=a - b;
Console.WriteLine("Line 2- Value of c is {0}", c);
c=a * b;
Console.WriteLine("Line 3- Value of c is {0}", c);
c=a/b;
Console.WriteLine("Line 4- Value of c is {0}", c);
c=a % b;
Console.WriteLine("Line 5- Value of c is {0}", c);
c=a++;
Console.WriteLine("Line 6- Value of c is {0}", c);
c=a--;
Console.WriteLine("Line 7- Value of c is {0}", c);
Console.ReadLine();
}
}
}编译并执行上述代码时,将生成以下输出-
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 22
Line 7 - Value of c is 20下表显示了C#支持的所有关系运算符。假设变量A=10,变量B=20,则-
| Operator | Remark | Example |
|---|---|---|
| == | 相等 | (A == B) is not true. |
| != | 不相等 | (A != B) is true. |
| > | 大于 | (A > B) is not true. |
| < | 小于 | (A < B) is true. |
| >= | 大于或等于 | (A >= B) is not true. |
| <= | 小于或等于 | (A <= B) is true. |
以下示例演示了C#-中可用的所有关系运算符
using System;
class Program {
static void Main(string[] args) {
int a=21;
int b=10;
if (a == b) {
Console.WriteLine("Line 1- a is equal to b");
} else {
Console.WriteLine("Line 1- a is not equal to b");
}
if (a < b) {
Console.WriteLine("Line 2- a is less than b");
} else {
Console.WriteLine("Line 2- a is not less than b");
}
if (a > b) {
Console.WriteLine("Line 3- a is greater than b");
} else {
Console.WriteLine("Line 3- a is not greater than b");
}
/* Lets change value of a and b */
a=5;
b=20;
if (a <= b) {
Console.WriteLine("Line 4- a is either less than or equal to b");
}
if (b >= a) {
Console.WriteLine("Line 5-b is either greater than or equal to b");
}
}
}编译并执行上述代码时,将生成以下输出-
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to b下表显示了C#支持的所有逻辑运算符。假设变量A=TRUE,变量B=FALSE,则-
| Operator | Remark | Example |
|---|---|---|
| && | 逻辑AND运算符,如果两个操作数都不为零,则为true。 | (A && B) is false. |
| || | 逻辑 OR 运算符,如果两个操作数中的任何一个都不为零,则为true。 | (A || B) is true. |
| ! | 逻辑非运算符,用于反转其操作数的逻辑状态。如果条件为true,则逻辑非运算符将为false。 | !(A && B) is true. |
以下示例演示了C#-中可用的所有逻辑运算符
using System;
namespace OperatorsAppl {
class Program {
static void Main(string[] args) {
bool a=true;
bool b=true;
if (a && b) {
Console.WriteLine("Line 1- Condition is true");
}
if (a || b) {
Console.WriteLine("Line 2- Condition is true");
}
/* lets change the value of a and b */
a=false;
b=true;
if (a && b) {
Console.WriteLine("Line 3- Condition is true");
} else {
Console.WriteLine("Line 3- Condition is not true");
}
if (!(a && b)) {
Console.WriteLine("Line 4- Condition is true");
}
Console.ReadLine();
}
}
}编译并执行上述代码时,将生成以下输出-
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true逐位运算符作用于位并执行逐位运算。&;、|和^的真值表如下所示:-
| p | q | p & q | p | q | p ^ q |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 |
假设A=60;B=13;则在二进制格式中,它们如下所示:-
A=0011 1100 B=0000 1101 ------------------- A&B =0000 1100 A|B =0011 1101 A^B =0011 0001 ~A? =1100 0011
下表列出了C#支持的按位运算符。假设变量A=60,变量B=13,则-
| Operator | Remark | Example |
|---|---|---|
| & | 二进制 AND 操作 | (A & B)=12, which is 0000 1100 |
| | | 二进制 OR 操作 | (A | B)=61, which is 0011 1101 |
| ^ | 二进制 XOR 异或 | (A ^ B)=49, which is 0011 0001 |
| ~ | 二进制补码 | (~A )=-61, which is 1100 0011 in 2's complement due to a signed binary number. |
| << | 二进制左移 | A << 2=240, which is 1111 0000 |
| >> | 二进制右移 | A >> 2=15, which is 0000 1111 |
using System;
namespace OperatorsAppl {
class Program {
static void Main(string[] args) {
int a=60; /* 60=0011 1100 */
int b=13; /* 13=0000 1101 */
int c=0;
c=a & b; /* 12=0000 1100 */
Console.WriteLine("Line 1- Value of c is {0}", c );
c=a | b; /* 61=0011 1101 */
Console.WriteLine("Line 2- Value of c is {0}", c);
c=a ^ b; /* 49=0011 0001 */
Console.WriteLine("Line 3- Value of c is {0}", c);
c=~a; /*-61=1100 0011 */
Console.WriteLine("Line 4- Value of c is {0}", c);
c=a << 2; /* 240=1111 0000 */
Console.WriteLine("Line 5- Value of c is {0}", c);
c=a >> 2; /* 15=0000 1111 */
Console.WriteLine("Line 6- Value of c is {0}", c);
Console.ReadLine();
}
}
}编译并执行上述代码时,将生成以下输出-
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15C#-支持以下赋值运算符
| Operator | Remark | Example |
|---|---|---|
| = | 赋值 | C=A + B assigns value of A + B into C |
| += | 先加再赋值 | C += A is equivalent to C=C + A |
| -= | 先减再赋值 | C-= A is equivalent to C=C- A |
| *= | 先乘再赋值 | C *= A is equivalent to C=C * A |
| /= | 先除再赋值 | C/= A is equivalent to C=C/A |
| %= | 先取模再赋值 | C %= A is equivalent to C=C % A |
| <<= | 左移赋值 | C <<= 2 is same as C=C << 2 |
| >>= | 右移赋值 | C >>= 2 is same as C=C >> 2 |
| &= | 按位AND赋值 | C &= 2 is same as C=C & 2 |
| ^= | 按位OR赋值 | C ^= 2 is same as C=C ^ 2 |
| |= | 按位非赋值 | C |= 2 is same as 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几乎没有其他重要的运算符,包括sizeof,typeof和?:。
| Operator | Remark | Example |
|---|---|---|
| sizeof() | 返回数据类型的大小。 | sizeof(int), returns 4. |
| typeof() | 返回类的类型。 | typeof(StreamReader); |
| & | 返回变量的地址。 | &a; returns actual address of the variable. |
| * | 指向变量的指针。 | *a; creates pointer named 'a' to a variable. |
| ? : | 条件表达式 | If Condition is true ? Then value X : Otherwise value Y |
| is | 确定对象是否属于某种类型。 | If( Ford is Car) //checks if Ford is an object of the Car class. |
| as | 如果强制转换失败,则不会引发异常。 | Object obj=new StringReader("Hello"); StringReader r=obj为StringReader; |
using System;
namespace OperatorsAppl {
class Program {
static void Main(string[] args) {
/* example of sizeof operator */
Console.WriteLine("The size of int is {0}", sizeof(int));
Console.WriteLine("The size of short is {0}", sizeof(short));
Console.WriteLine("The size of double is {0}", sizeof(double));
/* example of ternary operator */
int a, b;
a=10;
b=(a == 1) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
b=(a == 10) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
Console.ReadLine();
}
}
}编译并执行上述代码时,将生成以下输出-
The size of int is 4
The size of short is 2
The size of double is 8
Value of b is 30
Value of b is 20
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)