C#五類運(yùn)算符使用表達(dá)式樹進(jìn)行操作
在 C# 中,算術(shù)運(yùn)算符,有以下類型
- 算術(shù)運(yùn)算符
- 關(guān)系運(yùn)算符
- 邏輯運(yùn)算符
- 位運(yùn)算符
- 賦值運(yùn)算符
- 其他運(yùn)算符
這些運(yùn)算符根據(jù)參數(shù)的多少,可以分作一元運(yùn)算符、二元運(yùn)算符、三元運(yùn)算符。本文將圍繞這些運(yùn)算符,演示如何使用表達(dá)式樹進(jìn)行操作。
對于一元運(yùn)算符和二元運(yùn)算符的 Expression 的子類型如下:
UnaryExpression; //一元運(yùn)算表達(dá)式 BinaryExpression; //二元運(yùn)算表達(dá)式
一,算術(shù)運(yùn)算符
| 運(yùn)算符 | 描述 |
|---|---|
| + | 把兩個(gè)操作數(shù)相加 |
| - | 從第一個(gè)操作數(shù)中減去第二個(gè)操作數(shù) |
| * | 把兩個(gè)操作數(shù)相乘 |
| / | 分子除以分母 |
| % | 取模運(yùn)算符,整除后的余數(shù) |
| ++ | 自增運(yùn)算符,整數(shù)值增加 1 |
| -- | 自減運(yùn)算符,整數(shù)值減少 1 |
+ 與 Add()
正常代碼
int a;
int b;
a = 100;
b = 200;
var ab = a + b;
Console.WriteLine(ab);使用表達(dá)式樹構(gòu)建
ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
// ab = a + b
BinaryExpression ab = Expression.Add(a, b);
// 打印 a + b 的值
MethodCallExpression method = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), ab);
Expression<Action<int, int>> lambda = Expression.Lambda<Action<int, int>>(method, a, b);
lambda.Compile()(100, 200);
Console.ReadKey();如果想復(fù)雜一些,使用 塊 來執(zhí)行:
ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
// 別忘記了賦值
BinaryExpression aa = Expression.Assign(a, Expression.Constant(100, typeof(int)));
BinaryExpression bb = Expression.Assign(b, Expression.Constant(200, typeof(int)));
// ab = a + b
BinaryExpression ab = Expression.Add(a, b);
// 打印 a + b 的值
MethodCallExpression method = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), ab);
// 以塊的形式執(zhí)行代碼,相當(dāng)于{ }
// 不需要糾結(jié)這里,后面會有詳細(xì)說明,重點(diǎn)是上面
var call = Expression.Block(new ParameterExpression[] { a, b }, aa, bb, method);
Expression<Action> lambda = Expression.Lambda<Action>(call);
lambda.Compile()();上面兩個(gè)示例,是使用表達(dá)式樹計(jì)算結(jié)果,然后還是使用表達(dá)式樹打印結(jié)果。
前者依賴外界傳入?yún)?shù)值,賦予 a、b,后者則全部使用表達(dá)式樹賦值和運(yùn)算。
那么,如何通過表達(dá)式樹執(zhí)行運(yùn)算,獲取執(zhí)行結(jié)果呢?
ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
// ab = a + b
BinaryExpression ab = Expression.Add(a, b);
Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(ab, a, b);
int result = lambda.Compile()(100, 200);
Console.WriteLine(result);
Console.ReadKey();這些區(qū)別在于如何編寫 Expression.Lambda()。
另外,使用 AddChecked() 可以檢查操作溢出。
- 與 Subtract()
與加法一致,此處不再贅述,SubtractChecked() 可以檢查溢出。
a - b ,結(jié)果是 100 。
ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
// ab = a - b
BinaryExpression ab = Expression.Subtract(a, b);
Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(ab, a, b);
int result = lambda.Compile()(200, 100);
Console.WriteLine(result);乘除、取模
乘法
// ab = a * b
BinaryExpression ab = Expression.Multiply(a, b);
// ab = 20000除法
// ab = a / b
BinaryExpression ab = Expression.Divide(a, b);
// ab = 2取模(%)
ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
// ab = a % b
BinaryExpression ab = Expression.Modulo(a, b);
Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(ab, a, b);
int result = lambda.Compile()(200, 150);
// ab = 50
Console.WriteLine(result);
Console.ReadKey();自增自減
自增自減有兩種模型,一種是 x++ 或 x--,另一種是 ++x 或 --x。
他們都是屬于 UnaryExpression 類型。
| 算術(shù)運(yùn)算符 | 表達(dá)式樹 | 說明 |
|---|---|---|
| x++ | Expression.PostIncrementAssign() | 后置 |
| x-- | Expression.PostDecrementAssign() | 后置 |
| ++x | Expression.PreIncrementAssign() | 前置 |
| --x | Expression.PreDecrementAssign() | 前置 |
巧記:Post 后置, Pre 前置;Increment 是加,Decrement是減;Assign與賦值有關(guān)(后面會說到);
x++ 與 x-- 的使用
int a = 10;
int b = 10;
a++;
b--;
Console.WriteLine(a);
Console.WriteLine(b); // int a,b;
ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
// a = 10,b = 10;
BinaryExpression setA = Expression.Assign(a, Expression.Constant(10));
BinaryExpression setB = Expression.Assign(b, Expression.Constant(10));
// a++
UnaryExpression aa = Expression.PostIncrementAssign(a);
// b--
UnaryExpression bb = Expression.PostDecrementAssign(b);
//Console.WriteLine(a);
//Console.WriteLine(b);
MethodCallExpression callA = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), a);
MethodCallExpression callB = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), b);
BlockExpression block = Expression.Block(
new ParameterExpression[] { a, b },
setA,
setB,
aa,
bb,
callA,
callB
);
Expression<Action> lambda = Expression.Lambda<Action>(block);
lambda.Compile()();
Console.ReadKey();如果想把參數(shù)從外面?zhèn)魅?,設(shè)置 a,b
// int a,b;
ParameterExpression a = Expression.Variable(typeof(int), "a");
ParameterExpression b = Expression.Variable(typeof(int), "b");
// a++
UnaryExpression aa = Expression.PostIncrementAssign(a);
// b--
UnaryExpression bb = Expression.PostDecrementAssign(b);
//Console.WriteLine(a);
//Console.WriteLine(b);
MethodCallExpression callA = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), a);
MethodCallExpression callB = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), b);
BlockExpression block = Expression.Block(
aa,
bb,
callA,
callB
);
Expression<Action<int, int>> lambda = Expression.Lambda<Action<int, int>>(block, a, b);
lambda.Compile()(10, 10);
Console.ReadKey();生成的表達(dá)式樹如下
.Lambda #Lambda1<System.Action`2[System.Int32,System.Int32]>(
System.Int32 $a,
System.Int32 $b) {
.Block() {
$a++;
$b--;
.Call System.Console.WriteLine($a);
.Call System.Console.WriteLine($b)
}
}為了理解一下 Expression.Block(),可以在這里學(xué)習(xí)一下(后面會說到 Block())。
// int a,b;
ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
ParameterExpression c = Expression.Variable(typeof(int), "c");
BinaryExpression SetA = Expression.Assign(a, c);
BinaryExpression SetB = Expression.Assign(b, c);
// a++
UnaryExpression aa = Expression.PostIncrementAssign(a);
// b--
UnaryExpression bb = Expression.PostDecrementAssign(b);
//Console.WriteLine(a);
//Console.WriteLine(b);
MethodCallExpression callA = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), a);
MethodCallExpression callB = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), b);
BlockExpression block = Expression.Block(
new ParameterExpression[] { a, b },
SetA,
SetB,
aa,
bb,
callA,
callB
);
Expression<Action<int>> lambda = Expression.Lambda<Action<int>>(block, c);
lambda.Compile()(10);
Console.ReadKey();為什么這里要多加一個(gè) c 呢?我們來看看生成的表達(dá)式樹
.Lambda #Lambda1<System.Action`1[System.Int32]>(System.Int32 $c) {
.Block(
System.Int32 $a,
System.Int32 $b) {
$a = $c;
$b = $c;
$a++;
$b--;
.Call System.Console.WriteLine($a);
.Call System.Console.WriteLine($b)
}
}觀察一下下面代碼生成的表達(dá)式樹
// int a,b;
ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
// a++
UnaryExpression aa = Expression.PostIncrementAssign(a);
// b--
UnaryExpression bb = Expression.PostDecrementAssign(b);
//Console.WriteLine(a);
//Console.WriteLine(b);
MethodCallExpression callA = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), a);
MethodCallExpression callB = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), b);
BlockExpression block = Expression.Block(
new ParameterExpression[] { a, b },
aa,
bb,
callA,
callB
);
Expression<Action<int, int>> lambda = Expression.Lambda<Action<int, int>>(block, a, b);
lambda.Compile()(10, 10);
Console.ReadKey();.Lambda #Lambda1<System.Action`2[System.Int32,System.Int32]>(
System.Int32 $a,
System.Int32 $b) {
.Block(
System.Int32 $a,
System.Int32 $b) {
$a++;
$b--;
.Call System.Console.WriteLine($a);
.Call System.Console.WriteLine($b)
}
}關(guān)于前置的自增自減,按照上面示例編寫即可,但是需要注意的是, ++x 和 --x ,是“先運(yùn)算后增/自減”。
二,關(guān)系運(yùn)算符
==、!=、>、<、>=、<=
C# 中的關(guān)系運(yùn)算符如下
| 運(yùn)算符 | 描述 |
|---|---|
| == | 檢查兩個(gè)操作數(shù)的值是否相等,如果相等則條件為真。 |
| != | 檢查兩個(gè)操作數(shù)的值是否相等,如果不相等則條件為真。 |
| > | 檢查左操作數(shù)的值是否大于右操作數(shù)的值,如果是則條件為真。 |
| < | 檢查左操作數(shù)的值是否小于右操作數(shù)的值,如果是則條件為真。 |
| >= | 檢查左操作數(shù)的值是否大于或等于右操作數(shù)的值,如果是則條件為真。 |
| <= | 檢查左操作數(shù)的值是否小于或等于右操作數(shù)的值,如果是則條件為真。 |
== 表示相等比較,如果是值類型和 string 類型,則比較值是否相同;如果是引用類型,則比較引用的地址是否相等。
其它的關(guān)系運(yùn)算符則是僅比較值類型的大小。
實(shí)例代碼
int a = 21;
int b = 10;
Console.Write("a == b:");
Console.WriteLine(a == b);
Console.Write("a < b :");
Console.WriteLine(a < b);
Console.Write("a > b :");
Console.WriteLine(a > b);
// 改變 a 和 b 的值
a = 5;
b = 20;
Console.Write("a <= b:");
Console.WriteLine(a <= b);
Console.Write("a >= b:");
Console.WriteLine(b >= a);
Console.ReadKey();使用表達(dá)式樹實(shí)現(xiàn)
// int a,b;
ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
// a = 21,b = 10;
BinaryExpression setA = Expression.Assign(a, Expression.Constant(21));
BinaryExpression setB = Expression.Assign(b, Expression.Constant(20));
// Console.Write("a == b:");
// Console.WriteLine(a == b);
MethodCallExpression call1 = Expression.Call(null,
typeof(Console).GetMethod("Write", new Type[] { typeof(string) }),
Expression.Constant("a == b:"));
MethodCallExpression call11 = Expression.Call(null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(bool) }),
Expression.Equal(a, b));
// Console.Write("a < b :");
// Console.WriteLine(a < b);
MethodCallExpression call2 = Expression.Call(null,
typeof(Console).GetMethod("Write", new Type[] { typeof(string) }),
Expression.Constant("a < b :"));
MethodCallExpression call22 = Expression.Call(null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(bool) }),
Expression.LessThan(a, b));
// Console.Write("a > b :");
// Console.WriteLine(a > b);
MethodCallExpression call3 = Expression.Call(null,
typeof(Console).GetMethod("Write", new Type[] { typeof(string) }),
Expression.Constant("a > b :"));
MethodCallExpression call33 = Expression.Call(null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(bool) }),
Expression.GreaterThan(a, b));
// 改變 a 和 b 的值
// a = 5;
// b = 20;
BinaryExpression setAa = Expression.Assign(a, Expression.Constant(5));
BinaryExpression setBb = Expression.Assign(b, Expression.Constant(20));
// Console.Write("a <= b:");
// Console.WriteLine(a <= b);
MethodCallExpression call4 = Expression.Call(null,
typeof(Console).GetMethod("Write", new Type[] { typeof(string) }),
Expression.Constant("a <= b:"));
MethodCallExpression call44 = Expression.Call(null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(bool) }),
Expression.LessThanOrEqual(a, b));
// Console.Write("a >= b:");
// Console.WriteLine(b >= a);
MethodCallExpression call5 = Expression.Call(null,
typeof(Console).GetMethod("Write", new Type[] { typeof(string) }),
Expression.Constant("a >= b:"));
MethodCallExpression call55 = Expression.Call(null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(bool) }),
Expression.GreaterThanOrEqual(a, b));
BlockExpression block = Expression.Block(new ParameterExpression[] { a, b },
setA,
setB,
call1,
call11,
call2,
call22,
call3,
call33,
setAa,
setBb,
call4,
call44,
call5,
call55
);
Expression<Action> lambda = Expression.Lambda<Action>(block);
lambda.Compile()();
Console.ReadKey();生成的表達(dá)式樹如下
.Lambda #Lambda1<System.Action>() {
.Block(
System.Int32 $a,
System.Int32 $b) {
$a = 21;
$b = 20;
.Call System.Console.Write("a == b:");
.Call System.Console.WriteLine($a == $b);
.Call System.Console.Write("a < b :");
.Call System.Console.WriteLine($a < $b);
.Call System.Console.Write("a > b :");
.Call System.Console.WriteLine($a > $b);
$a = 5;
$b = 20;
.Call System.Console.Write("a <= b:");
.Call System.Console.WriteLine($a <= $b);
.Call System.Console.Write("a >= b:");
.Call System.Console.WriteLine($a >= $b)
}
}三,邏輯運(yùn)算符
&&、||、!
| 運(yùn)算符 | 描述 |
|---|---|
| && | 稱為邏輯與運(yùn)算符。如果兩個(gè)操作數(shù)都非零,則條件為真。 |
| || | 稱為邏輯或運(yùn)算符。如果兩個(gè)操作數(shù)中有任意一個(gè)非零,則條件為真。 |
| ! | 稱為邏輯非運(yùn)算符。用來逆轉(zhuǎn)操作數(shù)的邏輯狀態(tài)。如果條件為真則邏輯非運(yùn)算符將使其為假。 |
邏輯運(yùn)算符的運(yùn)行,結(jié)果是 true 或 false。
| 邏輯運(yùn)算符 | 表達(dá)式樹 |
|---|---|
| && | Expression.AndAlso() |
| || | Expression.OrElse() |
| ! | Expression.Not() |
int a = 10;
int b = 11;
Console.Write("[a == b && a > b]:");
Console.WriteLine(a == b && a > b);
Console.Write("[a > b || a == b]:");
Console.WriteLine(a > b || a == b);
Console.Write("[!(a == b)]:");
Console.WriteLine(!(a == b));
Console.ReadKey();使用表達(dá)式樹編寫
//int a = 10;
//int b = 11;
ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
BinaryExpression setA = Expression.Assign(a, Expression.Constant(10));
BinaryExpression setB = Expression.Assign(b, Expression.Constant(11));
//Console.Write("[a == b && a > b]:");
//Console.WriteLine(a == b && a > b);
MethodCallExpression call1 = Expression.Call(null, typeof(Console).GetMethod("Write", new Type[] { typeof(string) }), Expression.Constant("[a == b && a > b]:"));
MethodCallExpression call2 = Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(bool) }),
Expression.AndAlso(Expression.Equal(a, b), Expression.GreaterThan(a, b))
);
//Console.Write("[a > b || a == b]:");
//Console.WriteLine(a > b || a == b);
MethodCallExpression call3 = Expression.Call(null, typeof(Console).GetMethod("Write", new Type[] { typeof(string) }), Expression.Constant("[a > b || a == b]:"));
MethodCallExpression call4 = Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(bool) }),
Expression.OrElse(Expression.Equal(a, b), Expression.GreaterThan(a, b))
);
//Console.Write("[!(a == b)]:");
//Console.WriteLine(!(a == b));
MethodCallExpression call5 = Expression.Call(null, typeof(Console).GetMethod("Write", new Type[] { typeof(string) }), Expression.Constant("[!(a == b)]:"));
MethodCallExpression call6 = Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(bool) }),
Expression.Not(Expression.Equal(a, b))
);
BlockExpression block = Expression.Block(
new ParameterExpression[] { a, b },
setA,
setB,
call1,
call2,
call3,
call4,
call5,
call6
);
Expression<Action> lambda = Expression.Lambda<Action>(block);
lambda.Compile()();
Console.ReadKey();生成的表達(dá)式樹如下
.Lambda #Lambda1<System.Action>() {
.Block(
System.Int32 $a,
System.Int32 $b) {
$a = 10;
$b = 11;
.Call System.Console.Write("[a == b && a > b]:");
.Call System.Console.WriteLine($a == $b && $a > $b);
.Call System.Console.Write("[a > b || a == b]:");
.Call System.Console.WriteLine($a == $b || $a > $b);
.Call System.Console.Write("[!(a == b)]:");
.Call System.Console.WriteLine(!($a == $b))
}
}四,位運(yùn)算符
&、|、^、~、<<、>>
| 運(yùn)算符 | 描述 | 實(shí)例 |
|---|---|---|
| & | 如果同時(shí)存在于兩個(gè)操作數(shù)中,二進(jìn)制 AND 運(yùn)算符復(fù)制一位到結(jié)果中。 | (A & B) 將得到 12,即為 0000 1100 |
| | | 如果存在于任一操作數(shù)中,二進(jìn)制 OR 運(yùn)算符復(fù)制一位到結(jié)果中。 | (A | B) 將得到 61,即為 0011 1101 |
| ^ | 如果存在于其中一個(gè)操作數(shù)中但不同時(shí)存在于兩個(gè)操作數(shù)中,二進(jìn)制異或運(yùn)算符復(fù)制一位到結(jié)果中。 | (A ^ B) 將得到 49,即為 0011 0001 |
| ~ | 按位取反運(yùn)算符是一元運(yùn)算符,具有"翻轉(zhuǎn)"位效果,即0變成1,1變成0,包括符號位。 | (~A ) 將得到 -61,即為 1100 0011,一個(gè)有符號二進(jìn)制數(shù)的補(bǔ)碼形式。 |
| << | 二進(jìn)制左移運(yùn)算符。左操作數(shù)的值向左移動右操作數(shù)指定的位數(shù)。 | A << 2 將得到 240,即為 1111 0000 |
| >> | 二進(jìn)制右移運(yùn)算符。左操作數(shù)的值向右移動右操作數(shù)指定的位數(shù)。 | A >> 2 將得到 15,即為 0000 1111 |
限于篇幅,就寫示例了。
| 位運(yùn)算符 | 表達(dá)式樹 |
|---|---|
| & | Expression.Add(Expression left, Expression right) |
| | | Expression.Or(Expression left, Expression right) |
| ^ | Expression.ExclusiveOr(Expression expression) |
| ~ | Expression.OnesComplement( Expression expression) |
| << | Expression.LeftShift(Expression left, Expression right) |
| >> | Expression.RightShift(Expression left, Expression right) |
五,賦值運(yùn)算符
| 運(yùn)算符 | 描述 | 實(shí)例 |
|---|---|---|
| = | 簡單的賦值運(yùn)算符,把右邊操作數(shù)的值賦給左邊操作數(shù) | C = A + B 將把 A + B 的值賦給 C |
| += | 加且賦值運(yùn)算符,把右邊操作數(shù)加上左邊操作數(shù)的結(jié)果賦值給左邊操作數(shù) | C += A 相當(dāng)于 C = C + A |
| -= | 減且賦值運(yùn)算符,把左邊操作數(shù)減去右邊操作數(shù)的結(jié)果賦值給左邊操作數(shù) | C -= A 相當(dāng)于 C = C - A |
| *= | 乘且賦值運(yùn)算符,把右邊操作數(shù)乘以左邊操作數(shù)的結(jié)果賦值給左邊操作數(shù) | C *= A 相當(dāng)于 C = C * A |
| /= | 除且賦值運(yùn)算符,把左邊操作數(shù)除以右邊操作數(shù)的結(jié)果賦值給左邊操作數(shù) | C /= A 相當(dāng)于 C = C / A |
| %= | 求模且賦值運(yùn)算符,求兩個(gè)操作數(shù)的模賦值給左邊操作數(shù) | C %= A 相當(dāng)于 C = C % A |
| <<= | 左移且賦值運(yùn)算符 | C <<= 2 等同于 C = C << 2 |
| >>= | 右移且賦值運(yùn)算符 | C >>= 2 等同于 C = C >> 2 |
| &= | 按位與且賦值運(yùn)算符 | C &= 2 等同于 C = C & 2 |
| ^= | 按位異或且賦值運(yùn)算符 | C ^= 2 等同于 C = C ^ 2 |
| |= | 按位或且賦值運(yùn)算符 | C |= 2 等同于 C = C | 2 |
限于篇幅,請自行領(lǐng)略... ...
| 運(yùn)算符 | 表達(dá)式樹 |
|---|---|
| = | Expression.Assign |
| += | Expression.AddAssign |
| -= | Expression.SubtractAssign |
| *= | Expression.MultiplyAssign |
| /= | Expression.DivideAssign |
| %= | Expression.ModuloAssign |
| <<= | Expression.LeftShiftAssign |
| >>= | Expression.RightShiftAssign |
| &= | Expression.AndAssign |
| ^= | Expression.ExclusiveOrAssign |
| |= | Expression.OrAssign |
^= ,注意有兩種意思一種是位運(yùn)算符的異或(ExclusiveOrAssign),一種是算術(shù)運(yùn)算符的冪運(yùn)算(PowerAssign)。
六,其他運(yùn)算符
| 運(yùn)算符 | 描述 | 實(shí)例 |
|---|---|---|
| sizeof() | 返回?cái)?shù)據(jù)類型的大小。 | sizeof(int),將返回 4. |
| typeof() | 返回 class 的類型。 | typeof(StreamReader); |
| & | 返回變量的地址。 | &a; 將得到變量的實(shí)際地址。 |
| * | 變量的指針。 | *a; 將指向一個(gè)變量。 |
| ? : | 條件表達(dá)式 | 如果條件為真 ? 則為 X : 否則為 Y |
| is | 判斷對象是否為某一類型。 | If( Ford is Car) // 檢查 Ford 是否是 Car 類的一個(gè)對象。 |
| as | 強(qiáng)制轉(zhuǎn)換,即使轉(zhuǎn)換失敗也不會拋出異常。 | Object obj = new StringReader("Hello"); StringReader r = obj as StringReader; |
到此這篇關(guān)于C#五類運(yùn)算符使用表達(dá)式樹進(jìn)行操作的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于C#實(shí)現(xiàn)一個(gè)最簡單的HTTP服務(wù)器實(shí)例
這篇文章主要介紹了基于C#實(shí)現(xiàn)一個(gè)最簡單的HTTP服務(wù)器的方法,詳細(xì)分析了http服務(wù)器的實(shí)現(xiàn)原理與相關(guān)技巧,以及對應(yīng)的注意事項(xiàng),需要的朋友可以參考下2014-12-12
C#中的SQLCommand命令與DbTransaction事務(wù)處理
這篇文章介紹了C#中的SQLCommand命令與DbTransaction事務(wù)處理,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
Unity3D實(shí)戰(zhàn)之答題系統(tǒng)的實(shí)現(xiàn)
本文將用Unity3D制作一個(gè)答題系統(tǒng),可以從文本文檔中提取題目和分?jǐn)?shù),然后綁定到UI上,在答題的過程中,自動判斷分?jǐn)?shù),自動判斷正確率。感興趣的可以學(xué)習(xí)一下2022-03-03
unity實(shí)現(xiàn)貼圖矩陣運(yùn)算(旋轉(zhuǎn)平移縮放)
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)貼圖矩陣運(yùn)算,旋轉(zhuǎn)平移縮放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
C#調(diào)用QQ_Mail發(fā)送郵件實(shí)例代碼兩例
這篇文章介紹了C#調(diào)用QQ_Mail發(fā)送郵件的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04

