最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#判斷語句的表達(dá)式樹實(shí)現(xiàn)

 更新時間:2022年01月18日 10:29:58   作者:癡者工良  
這篇文章介紹了C#判斷語句的表達(dá)式樹實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

C# 提供了以下類型的判斷語句:

語句描述
if一個 if 語句 由一個布爾表達(dá)式后跟一個或多個語句組成。
if...else一個 if 語句 后可跟一個可選的 else 語句,else 語句在布爾表達(dá)式為假時執(zhí)行。
嵌套 if 語句您可以在一個 if 或 else if 語句內(nèi)使用另一個 if 或 else if 語句。
switch 語句一個 switch 語句允許測試一個變量等于多個值時的情況。
嵌套 switch 語您可以在一個 switch 語句內(nèi)使用另一個 switch 語句。

當(dāng)然還有 ??、?: 等判斷,下面將詳細(xì)實(shí)踐。

if

If 語句,使用 IfThen(Expression test, Expression ifTrue); 來表達(dá)

Expression test表示用于判斷的表達(dá)式,Expression ifTrue表示結(jié)果為 true 時執(zhí)行的表達(dá)式樹。

示例

            int a = 10;
            int b = 10;
            
            if (a == b)
            {
                Console.WriteLine("a == b 為 true,語句被執(zhí)行");
            }

            Console.ReadKey();

使用表達(dá)式樹實(shí)現(xiàn)如下

            ParameterExpression a = Expression.Variable(typeof(int), "a");
            ParameterExpression b = Expression.Variable(typeof(int), "b");
            MethodCallExpression call = Expression.Call(
                null,
                typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
                Expression.Constant("a == b 為 true,表達(dá)式樹被執(zhí)行"));

            ConditionalExpression _if = Expression.IfThen(Expression.Equal(a, b),call);

            Expression<Action<int, int>> lambda = Expression.Lambda<Action<int, int>>(_if,a,b);
            lambda.Compile()(10,10);

            Console.ReadKey();

生成的表達(dá)式樹如下

.Lambda #Lambda1<System.Action`2[System.Int32,System.Int32]>(
    System.Int32 $a,
    System.Int32 $b) {
    .If ($a == $b) {
        .Call System.Console.WriteLine("a == b 為 true,表達(dá)式樹被執(zhí)行")
    } .Else {
        .Default(System.Void)
    }
}

if...else

if...else 使用以下表達(dá)式樹表示

 ConditionalExpression IfThenElse(Expression test, Expression ifTrue, Expression ifFalse);

示例代碼如下

            int a = 10;
            int b = 11;

            if (a == b)
            {
                Console.WriteLine("a == b 為 true,此語句被執(zhí)行");
            }
            else
            {
                Console.WriteLine("a == b 為 false,此語句被執(zhí)行");
            }
            Console.ReadKey();

用表達(dá)式樹實(shí)現(xiàn)如下

            ParameterExpression a = Expression.Variable(typeof(int), "a");
            ParameterExpression b = Expression.Variable(typeof(int), "b");
            MethodCallExpression call1 = Expression.Call(
                null,
                typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
                Expression.Constant("a == b 為 true,此表達(dá)式樹被執(zhí)行"));

            MethodCallExpression call2 = Expression.Call(
                null,
                typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
                Expression.Constant("a == b 為 false,此表達(dá)式樹被執(zhí)行"));

            ConditionalExpression _if = Expression.IfThenElse(Expression.Equal(a, b), call1,call2);

            Expression<Action<int, int>> lambda = Expression.Lambda<Action<int, int>>(_if, a, b);
            lambda.Compile()(10, 11);

            Console.ReadKey();

生成的表達(dá)式樹如下

.Lambda #Lambda1<System.Action`2[System.Int32,System.Int32]>(
    System.Int32 $a,
    System.Int32 $b) {
    .If ($a == $b) {
        .Call System.Console.WriteLine("a == b 為 true,此表達(dá)式樹被執(zhí)行")
    } .Else {
        .Call System.Console.WriteLine("a == b 為 false,此表達(dá)式樹被執(zhí)行")
    }
}

switch

示例代碼如下

            int a = 2;
            switch (a)
            {
                case 1:Console.WriteLine("a == 1");break;
                case 2:Console.WriteLine("a == 2");break;
                default:Console.WriteLine("a != 1 && a = 2");
            }

            Console.ReadKey();

每個 case 使用 SwitchCase 類型表示,使用 Expression.SwitchCase 生成 SwitchCase 類型。

Expression.Switch 用來構(gòu)建一個 switch 表達(dá)式樹,

Expression.Switch 的重載比較多,常用的是這種形式

SwitchExpression Switch(Expression switchValue, Expression defaultBody, params SwitchCase[] cases);

switchValue 表示傳入?yún)?shù);

defaultBody 表示 default 執(zhí)行的表達(dá)式;

cases 表示多條 case 。

上面代碼對應(yīng)使用表達(dá)式樹編寫如下

            ParameterExpression a = Expression.Parameter(typeof(int), "a");
            MethodCallExpression _default = Expression.Call(
                null,
                typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
                Expression.Constant("a != 1 && a = 2"));

            SwitchCase case1 = Expression.SwitchCase(
                Expression.Call(null,
                typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
                Expression.Constant("a == 1")),
                new ConstantExpression[] { Expression.Constant(1) }
                );

            SwitchCase case2 = Expression.SwitchCase(
                Expression.Call(null,
                typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
                Expression.Constant("a == 2")),
                new ConstantExpression[] { Expression.Constant(2) }
                );

            SwitchExpression _switch = Expression.Switch(a, _default, new SwitchCase[] { case1, case2 });
            Expression<Action<int>> lambda = Expression.Lambda<Action<int>>(_switch, a);
            lambda.Compile()(1);

            Console.ReadKey();

生成的表達(dá)式樹如下

.Lambda #Lambda1<System.Action`1[System.Int32]>(System.Int32 $a) {
    .Switch ($a) {
    .Case (1):
            .Call System.Console.WriteLine("a == 1")
    .Case (2):
            .Call System.Console.WriteLine("a == 2")
    .Default:
            .Call System.Console.WriteLine("a != 1 && a = 2")
    }
}

很奇怪,沒有 break,但是表達(dá)式樹是正常的,并且運(yùn)行沒問題;

?? 和 ?:

?? 表示空合并運(yùn)算符,例如 a ?? b,如果 a 不為 null,即返回 a,否則返回 b;

常用定義如下

BinaryExpression Coalesce(Expression left, Expression right)

這里就不再贅述。

?: 是三元運(yùn)算符,例如 a > b ? a : b 。

常用定義如下

ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse)

可以參考上面的 if...else 表達(dá)式樹,這里不再贅述。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#實(shí)現(xiàn)同步模式下的端口映射程序

    C#實(shí)現(xiàn)同步模式下的端口映射程序

    這篇文章介紹了C#實(shí)現(xiàn)同步模式下的端口映射程序,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C#實(shí)現(xiàn)串口調(diào)試工具

    C#實(shí)現(xiàn)串口調(diào)試工具

    這篇文章介紹了C#實(shí)現(xiàn)串口調(diào)試工具的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • Unity實(shí)現(xiàn)俄羅斯方塊

    Unity實(shí)現(xiàn)俄羅斯方塊

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • C#中的Hashtable?類使用詳解

    C#中的Hashtable?類使用詳解

    這篇文章主要介紹了C#中的Hashtable?類使用詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • 基于C#中XmlWriter寫入Xml的深入分析

    基于C#中XmlWriter寫入Xml的深入分析

    本篇文章是對C#中XmlWriter寫入Xml進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C# 三種序列化方法分享

    C# 三種序列化方法分享

    這篇文章主要介紹了C# 三種序列化方法,需要的朋友可以參考下
    2014-02-02
  • C#編程實(shí)現(xiàn)DataTable添加行的方法

    C#編程實(shí)現(xiàn)DataTable添加行的方法

    這篇文章主要介紹了C#編程實(shí)現(xiàn)DataTable添加行的方法,結(jié)合兩個實(shí)例形式分析了C#操作DataTable實(shí)現(xiàn)動態(tài)添加行的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • C#中委托的基礎(chǔ)入門與實(shí)現(xiàn)方法

    C#中委托的基礎(chǔ)入門與實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于C#中委托的基礎(chǔ)入門與實(shí)現(xiàn)方法的相關(guān)資料,究竟什么是委托,用最通俗易懂的話來講,你就可以把委托看成是用來執(zhí)行方法(函數(shù))的一個東西,需要的朋友可以參考下
    2021-08-08
  • WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新聞效果的方法

    WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新聞效果的方法

    這篇文章主要介紹了WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新聞效果的方法,涉及WinForm窗口滾動字幕設(shè)置的實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • 簡單的觀察者模式示例分享

    簡單的觀察者模式示例分享

    這篇文章主要介紹了簡單的觀察者模式示例,抽象層定義了觀察者模式,實(shí)現(xiàn)層是對抽象層的具體實(shí)現(xiàn),需要的朋友可以參考下
    2014-03-03

最新評論

永年县| 墨竹工卡县| 平果县| 湘潭县| 桦甸市| 周至县| 广灵县| 靖江市| 泾阳县| 全州县| 唐山市| 盘锦市| 天长市| 桐乡市| 古蔺县| 遵义县| 莱阳市| 永川市| 濉溪县| 东源县| 昆明市| 荃湾区| 崇礼县| 沁源县| 天等县| 历史| 泰宁县| 盱眙县| 祁东县| 江津市| 江门市| 滨州市| 东山县| 连山| 蒙自县| 沈丘县| 汤阴县| 仙居县| 梅河口市| 克东县| 都昌县|