鏈€杩戝彂鐜?C#7涔嬪悗鐨?is鏄秺鏉ヨ秺鐪嬩笉鎳備簡锛屼箥涓€鐪嬭姳閲岃儭鍝ㄧ殑锛屼笉杩囧綋鎴戦潤涓嬪績鏉ヤ粩缁嗙爺璇伙紝鍙戠幇杩?is鏄秺鏉ヨ秺鐭皬绮炬倣锛岃€屼笖杩樼壒鍒涔夊寲锛岄偅鎬庢槸涓€涓埥瀛椾簡寰楌煒勶紝杩欎竴绡囧氨鍜屽ぇ瀹剁畝鍗曡亰涓€鑱娿€?/p>浜岋細C#7涔嬪墠鐨?is濡備綍浣跨敤1.绫诲"/>

Posted Dotnet9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。

涓€锛氳儗鏅?/span>

1. 璁叉晠浜?span class="mq-7">

鏈€杩戝彂鐜?C#7 涔嬪悗鐨?is 鏄秺鏉ヨ秺鐪嬩笉鎳備簡锛屼箥涓€鐪嬭姳閲岃儭鍝ㄧ殑锛屼笉杩囧綋鎴戦潤涓嬪績鏉ヤ粩缁嗙爺璇伙紝鍙戠幇杩?is 鏄秺鏉ヨ秺鐭皬绮炬倣锛岃€屼笖杩樼壒鍒涔夊寲锛岄偅鎬庢槸涓€涓埥瀛椾簡寰楌煒勶紝杩欎竴绡囧氨鍜屽ぇ瀹剁畝鍗曡亰涓€鑱娿€?/p>

浜岋細C#7 涔嬪墠鐨?is 濡備綍浣跨敤

1. 绫诲瀷鍏煎鎬ф娴?span class="mq-15">

鐩镐俊瀛﹁繃 C# 鐨勬湅鍙嬮兘浼氱煡閬?is 鏄共鍢涚殑锛岃€屼笖杩樼粡甯稿拰 as 涓€璧锋瘮杈冿紝鍓嶈€呬竴鑸仛鍏煎鎬ф娴嬶紝鍚庤€呬竴鑸仛鍏煎鎬ц浆鎹紝杩欓噷鎴戝氨涓句釜渚嬪瓙鍚э細


        static void Main(string[] args)
        {
            object slot = new Slot() { ClothesName = "涓婅。" };

            if (slot is Slot)
            {
                Console.WriteLine($"slot is {nameof(Slot)}");
            }

            if (slot is IComparable)
            {
                Console.WriteLine($"slot is {nameof(IComparable)}");
            }
        }

        public class Slot : IComparable
        {
            public string ClothesName { getset; }

            public int CompareTo(object obj) {return 0;}
        }

浠庤繖涓緥瀛愬彲浠ョ湅鍒帮紝 object 绫诲瀷鐨?slot 鍜?Slot, IComparable 閮芥槸绫诲瀷鍏煎鐨勶紝闈炲父绠€鍗曘€?/p>

2. 閬楁喚鐨勫湴鏂?span class="mq-58">

鐒惰€屽湪瀹為檯缂栫爜涓紝鎴戠浉淇℃湁寰堝鏈嬪弸閮戒細鍦ㄥ悗缁殑杩囩▼涓敤鍒?slot 瀹炰緥锛岃€屼笂闈㈢殑杩欎釜渚嬪瓙锛屽嵆浣挎垜鐢?is 妫€娴嬪埌浜嗘槸 Slot 绫诲瀷锛屾渶鍚庢垜杩樻槸瑕?灏?object slot 寮鸿浆鎴?Slot绫诲瀷锛屽仛浜嗕竴娆℃娴嬶紝鍙堝仛浜嗕竴涓己杞紝杩欏氨寰堝钁╀簡锛屽涓嬩唬鐮侊細


            if (slot is Slot)
            {
                var query = (Slot)slot;
                Console.WriteLine($"slot is {nameof(Slot)}, ClothesName={query.ClothesName}");
            }

闄ら潪鏈夋瘺鐥呮墠鍐欒繖鏍风殑浠g爜锛屽共鍢涗笉鐩存帴鐢?as 灏濊瘯鎬ц浆鎹㈠皢涓ゆ鍚堜负涓€姝ヨ蛋鍛紵淇敼浠g爜濡備笅锛?/p>


            var query = slot as Slot;

            if (query != null)
            {
                Console.WriteLine($"slot is {nameof(Slot)}, ClothesName={query.ClothesName}");
            }

C# 涓殑 is 鐪熺殑鏄秺鏉ヨ秺寮哄ぇ锛岃秺鏉ヨ秺璇箟鍖?> 
 </figure> 
 <p data-tool=杩欏氨瀵艰嚧寰堝鍦烘櫙涓嬶紝is 閮借 as 鏇夸唬浜嗭紝鎼炵殑 is 鎴愪簡涓€涓┖鏋跺瓙锛屽鏋?is 鑳藉悎骞?as 鐨勫姛鑳斤紝閭e氨馃悅馃憙浜嗭紝鎴戣寰楄繖涓€ラ渶澧炲己銆?/p>

涓夛細C#7 涔嬪悗鐨?is 濡備綍浣跨敤

涔熺粓浜庡湪 C#7 涔嬪悗瀵?is 杩涜浜嗙炕澶╄鍦扮殑璇硶绯栨敼閫狅紝瀵艰嚧浣犲垵鐪嬪凡缁忎笉鏄庣櫧鍟︷煒勷煒勷煒勶紝涓嬮潰鎴戝氨涓€涓€涓句緥鏉ヨ鏄庡惂銆?/p>

1. is 鍜?澶嶆潅绫诲瀷/绠€鍗曠被鍨?鐨勭粨鍚?span class="mq-90">

鐜板湪灏辨潵鐪嬩竴涓嬫€庝箞鐢ㄦ柊is 瑙e喅鍒氭墠涓ゆ杞崲鐨勯棶棰橈紝濡備笅浠g爜锛?/p>


            object slot = new Slot() { ClothesName = "涓婅。" };

            if(slot is Slot query)
            {
                Console.WriteLine($"slot is {nameof(Slot)}, ClothesName={query.ClothesName}");
            }

C# 涓殑 is 鐪熺殑鏄秺鏉ヨ秺寮哄ぇ锛岃秺鏉ヨ秺璇箟鍖?> 
 </figure> 
 <p data-tool=杩欐浠g爜琛ㄩ潰鎰忔€濇槸锛氬厛鐢?is 妫€娴?slot 鏄惁涓?Slot 绫诲瀷锛屽鏋滄槸灏辫祴鍊肩粰 Slot 绫诲瀷鐨?query 鍙橀噺锛屽搱鍝堬紝鏈夌偣鎰忔€濆惂锛屼负浜嗛獙璇佹槸鍚﹀鎴戞墍璇达紝鐢ㄥ弽缂栬瘧宸ュ叿鐪嬬湅銆?/p>
  • ILSpy 鍙嶇紪璇?
C# 涓殑 is 鐪熺殑鏄秺鏉ヨ秺寮哄ぇ锛岃秺鏉ヨ秺璇箟鍖?> 
 </figure> 
 <ul data-tool=
  • DnSpy 鍙嶇紪璇?
  • C# 涓殑 is 鐪熺殑鏄秺鏉ヨ秺寮哄ぇ锛岃秺鏉ヨ秺璇箟鍖?> 
 </figure> 
 <p data-tool=鍙互鐪嬪埌锛屽湪瀹炴搷涓紝缂栬瘧鍣ㄩ兘鐢?as 杩涜浜嗚繕鍘燂紝涓嶈繃浠庝唬鐮佹祦鐣呮€ф潵鐪嬶紝ILSpy鏇答煇傪煈冧竴鐐广€?/p>

    闄や簡鍜岀被瀹炰緥姣旇緝涔嬪锛岃繕鍙互鍜?int,string,tuple ...杩涜姣旇緝锛?浠g爜濡備笅锛?/p>


                object e = 150;

                //瀛楃涓叉瘮杈?/span>
                if (e is "hello") { }

                //鏁村舰姣旇緝
                if (e is 10) { }

                //tuple 姣旇緝
                if (is (1020)) { }

    2. is 鍜?null 鐨勭粨鍚?span class="mq-137">

    澶у鍦ㄥ啓 sql 鐨勬椂鍊欏垽鏂煇涓€涓瓧娈垫槸鍚︿负 null锛岄€氬父閮戒細杩欐牱鍐欙細username is null 鎴栬€?username is not null 锛屽搱鍝堬紝杩欑鍐欐硶涔熻寮曞叆鍒?C# 涓簡锛屾湁鎰忔€濆惂锛屼笂浠g爜锛?/p>


                object e = 150;

                if (e is null)
                {
                    Console.WriteLine("e is null");
                }

                if (e is not null)
                {
                    Console.WriteLine("e is not null");
                }

    杩欎箞璇箟鍖栫殑鍐欐硶鍦–#涓湅鍒版槸涓嶆槸鏈夌偣涓嶄範鎯紝閭d负鍟ュ湪 sql 涓氨涔犱互涓哄父鍛紵鍏跺疄鍙嶇紪璇戣繃鏉ヤ篃娌″暐锛屽氨鏄竴涓?== 鍒ゆ柇锛屽涓嬩唬鐮侊細

    C# 涓殑 is 鐪熺殑鏄秺鏉ヨ秺寮哄ぇ锛岃秺鏉ヨ秺璇箟鍖?> 
 </figure> 
 <h3 data-tool=3. is 鍜?and 锛宱r 鐨勭粨鍚?span class="mq-158">

    鐜板湪澶у閮界湅鍒颁簡 is 閫氬父鏄斁鍦?if 璇彞涓紝鏃㈢劧鍦?if 璇彞涓紝閭h偗瀹氭湁寰堝鐨勯€昏緫鍒ゆ柇锛岃繖灏遍渶瑕佺粨鍚?and锛宱r 鏋勫缓闈炲父澶嶆潅鐨勯€昏緫鍏崇郴锛屼笉瑕佺溂鑺卞摝銆?/p>


                object e = 150;

                if (e is >= 100 and <= 200)
                {
                    Console.WriteLine($"e={e} 鏋滅劧 澶т簬 100 骞朵笖 灏忎簬 200");
                }

                if (e is 100 or 150 or 200)
                {
                    Console.WriteLine($"e={e} 鏄湪 100,150,200 涓変釜鏁板瓧涓?);
                }

                if (e is not null and not "")
                {
                    Console.WriteLine($"e={e}锛屾ā鎷?nbsp;!string.IsNullOrEmpty 鍔熻兘");
                }

    C# 涓殑 is 鐪熺殑鏄秺鏉ヨ秺寮哄ぇ锛岃秺鏉ヨ秺璇箟鍖?> 
 </figure> 
 <p data-tool=鍙互鐪嬪埌鏈€鍚庣殑: e is not null and not "" 鍏跺疄绛変环浜?!string.IsNullOrEmpty, 鏄笉鏄湁鐐规剰鎬濆搱銆?/p>

    杩欓噷鏈変竴鐐硅鎻愰啋鐨勬槸锛屼笂闈㈢殑 e 鍦ㄧ紪璇戝櫒灞傞潰閮芥槸 object 绫诲瀷锛屽鏋滀綘鎯冲湪 缂栬瘧鍣ㄥ眰闈娇鐢?int 杩愪綔锛岃繕鏄敤 渚嬪瓙1 鐨勬柟寮忚浆鎹竴涓嬪搱锛屽涓嬪浘鎵€绀猴細

    4. is 鍜?var 鐨勭粨鍚?span class="mq-192">

    褰?is 鍜?var 缁撳悎璧锋潵灏辨洿馃悅馃憙浜嗭紝鍙互瀹炵幇鍦?if 鍒ゆ柇鐨勮繃绋嬩腑鐢熸垚涓存椂鍙橀噺锛屽涓嬩唬鐮侊細


                int e = 150;

                if (e is var i && i >= 100 && i <= 200)
                {
                    Console.WriteLine($"e={i} 鏋滅劧 澶т簬 100 骞朵笖 灏忎簬 200");
                }

    涓婇潰浠g爜涓殑 i 灏辨槸涓€涓复鏃跺彉閲忥紝鍚庨潰鍋氱殑涓€鍒囦笟鍔¢€昏緫閮芥槸鍩轰簬 i 杩欎釜涓存椂鍙橀噺鐨勶紝濡傛灉杩樻病鏈夐浼氬埌绮剧补锛屾病鍏崇郴锛屾垜涓句竴涓」鐩腑鐨勪緥瀛愬惂銆傘€傘€?/p>

    鎴戜滑鍏徃鏄悶琛g墿娲楁钉鑷姩鍖栵紝涔熼渶瑕佸绾夸笅 浼犺緭绾夸笂鐨勮。鏈嶈繘琛岃嚜鍔ㄥ寲涓婃寕锛屽彇璧板拰琛g墿缁勫悎鎼厤锛屼妇涓緥瀛愶細鎵惧埌 鍒氬ソ鎸備簡涓€浠惰¥瀛怢 && 涓€浠朵笂琛 && 鎬昏。鏈嶄釜鏁?2 鐨?鎸傚瓟鍙?/code>锛岃鏄繕娌″惉鎳傚氨绠椾簡锛岀洿鎺ヤ笂浠g爜璇磋瘽銆?/p>


    class Program
        {
            static void Main(string[] args)
            {
                var slotList = new List<Slot>()
                {
                    new Slot()  {SlotID=1, ClothesID=10,ClothesName="涓婅。", SizeName= "L" },
                    new Slot()  {SlotID=1, ClothesID=20,ClothesName="瑁ゅ瓙", SizeName= "M" },
                    new Slot()  {SlotID=1, ClothesID=11,ClothesName="鐨甫", SizeName= "X" },
                    new Slot()  {SlotID=2, ClothesID=30,ClothesName="涓婅。", SizeName= "L" },
                    new Slot()  {SlotID=2, ClothesID=40,ClothesName="瑁ゅ瓙", SizeName= "L" }
                };

                //鎵惧埌 鍒氬ソ鎸備簡涓€浠惰¥瀛怢 & 涓€浠朵笂琛  & 鎬昏。鏈嶄釜鏁?2  鐨?nbsp;鎸傚瓟鍙?/span>
                var query = slotList.GroupBy(m => m.SlotID).Where(m =>
                                                                          m.Where(n => n.SizeName == "L").ToList() is var clothesList &&
                                                                          clothesList.Count(k => k.ClothesName == "瑁ゅ瓙"is 1 &&
                                                                          clothesList.Count(k => k.ClothesName == "涓婅。"is 1 &&
                                                                          m.Key == 2
                                                                  )
                                                        .ToDictionary(k => k.Key, v => v.ToList());
            }

            public class Slot
            {
                public int SlotID { getset; }

                public int ClothesID { getset; }

                public string ClothesName { getset; }

                public string SizeName { getset; }
            }
        }

    閲嶇偣鍦ㄤ簬涓婇潰浠g爜鐨? m.Where(n => n.SizeName == "L").ToList() is var clothesList锛岃繖閲岀殑 clothesList 灏辨槸涓存椂鍙橀噺锛屽畠瀛樻斁浜嗘墍鏈?灏哄L 鐨勮。鐗?鍚庣画鐨勬绱㈤兘鏄熀浜庤繖涓?clothesList锛屾槸涓嶆槸澶уぇ鎻愰珮浜嗘绱㈤€熷害~~~

    鍥涳細鎬荤粨

    鎴戣寰?is 鐨勫姛鑳藉寮烘棭灏辫鍑虹幇浜嗭紝鐜板湪缁堜簬鎼炲畾浜嗭紝瓒婃潵瓒婁汉鎬у寲锛岄敭鐩樻暡鍑绘鏁拌秺鏉ヨ秺灏戯紝澶村彂涔熶笉钀戒簡锛岀敋鑷冲張寮€濮嬬浜屾槬浜嗭紝鎬荤殑鏉ヨ杩樻槸閭e彞璇濓紝C# 澶ф硶馃悅馃憙銆?/p>

    以上是关于的主要内容,如果未能解决你的问题,请参考以下文章

    谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

    片段和活动之间的核心区别是啥?哪些代码可以写成片段?

    VSCode自定义代码片段——.vue文件的模板

    VSCode自定义代码片段6——CSS选择器

    VSCode自定义代码片段——声明函数

    VSCode自定义代码片段8——声明函数