久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C# Null 傳播運算符/條件訪問表達式 &如果塊

C# Null propagating operator / Conditional access expression amp; if blocks(C# Null 傳播運算符/條件訪問表達式 amp;如果塊)
本文介紹了C# Null 傳播運算符/條件訪問表達式 &如果塊的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

空傳播運算符/條件訪問表達式tagged/c%23-6.0" class="post-tag" title="show questions tagged 'c#-6.0'" rel="tag">c#-6.0 看起來非常方便.但我很好奇它是否有助于解決檢查子成員是否為空然后在 if 塊內(nèi)對所述子成員調(diào)用布爾方法的問題:

The Null propagating operator / Conditional access expression coming in c#-6.0 looks like quite a handy feature. But I'm curious if it will help solve the problem of checking if a child member is not null and then calling a Boolean method on said child member inside an if block:

  public class Container<int>{
       IEnumerable<int> Objects {get;set;}
  }

  public Container BuildContainer()
  { 
      var c = new Container();

      if (/* Some Random Condition */)
         c.Objects = new List<int>{1,2,4};
  }

  public void Test()
  {
      var c = BuildContainer();

      //Old way
      if ( null != c && null != c.Objects && c.Objects.Any())
         Console.Write("Container has items!");


      //C# 6 way?
      if (c?.Object?.Any())
          Console.Write("Container has items!");
  }

c?.Object?.Any() 會編譯嗎?如果傳播運算符短路(我認為這是正確的術語)為空,那么您有 if (null),這是無效的.

Will c?.Object?.Any() compile? If the propagating operator short circuits (I assume that's the right term) to null then you have if (null), which isn't valid.

C# 團隊會解決這個問題還是我錯過了空傳播運算符的預期用例?

Will the C# team address this concern or am I missing the intended use case for the null propagating operator?

推薦答案

這樣不行.您可以跳過解釋并查看下面的代碼:)

It won't work this way. You can just skip the explanation and see the code below :)

如您所知 ?. 如果子成員為 null,運算符將返回 null.但是如果我們嘗試獲取一個不可為空的成員,比如 Any() 方法,它返回 bool 會發(fā)生什么?答案是編譯器會將返回值包裝"在 Nullable<> 中.例如,Object?.Any() 會給我們 bool?(即 Nullable),而不是 bool.

As you know ?. operator will return null if a child member is null. But what happens if we try to get a non-nullable member, like the Any() method, that returns bool? The answer is that the compiler will "wrap" a return value in Nullable<>. For example, Object?.Any() will give us bool? (which is Nullable<bool>), not bool.

唯一不允許我們在 if 語句中使用這個表達式的是它不能被隱式轉(zhuǎn)換為 bool.但是您可以明確地進行比較,我更喜歡像這樣與 true 進行比較:

The only thing that doesn't let us use this expression in the if statement is that it can't be implicitly casted to bool. But you can do comparison explicitly, I prefer comparing to true like this:

if (c?.Object?.Any() == true)
    Console.Write("Container has items!");

感謝@DaveSexton 還有另一種方式:

if (c?.Object?.Any() ?? false)
    Console.Write("Container has items!");

但對我來說,與 true 的比較似乎更自然:)

But as for me, comparison to true seems more natural :)

這篇關于C# Null 傳播運算符/條件訪問表達式 &amp;如果塊的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

How to check if String is null(如何檢查字符串是否為空)
Equals(item, null) or item == null(Equals(item, null) 或 item == null)
Overriding == operator. How to compare to null?(覆蓋 == 運算符.如何與空值進行比較?)
What does the question mark in member access mean in C#?(成員訪問中的問號在 C# 中是什么意思?)
The || (or) Operator in Linq with C#(||(或)C# 中的 Linq 運算符)
C# null coalescing operator equivalent for c++(C# 空合并運算符等效于 C++)
主站蜘蛛池模板: 欧美天堂 | 激情网五月天 | japanhd成人| a在线视频| 久久国产一区二区三区 | 国产精品久久国产精品 | 国产女人与拘做受免费视频 | 91高清在线 | 久久久青草婷婷精品综合日韩 | 亚洲欧美日韩精品久久亚洲区 | 亚洲日本视频 | 欧美电影一区 | 日本精品视频在线 | 亚洲精品无 | 国产电影一区二区 | 高清免费av | 国产一区二区精品在线观看 | 中文字幕一区二区三区日韩精品 | 成人av在线播放 | 嫩呦国产一区二区三区av | 亚洲传媒在线 | 亚洲国产成人一区二区 | 亚洲视频在线看 | 国产精品九九视频 | 中文字幕11页 | 国产午夜精品一区二区三区四区 | 三级av免费| 日韩电影a| 91国在线 | 精品日韩一区二区 | 精品一二区 | 欧美亚洲日本 | 一区二区三区精品视频 | 国产精品国产a级 | 亚洲视频一 | 欧美视频中文字幕 | 国产精品中文字幕在线播放 | 奇米影视在线 | 亚洲一区二区三区四区在线观看 | 99久久日韩精品免费热麻豆美女 | www日日日 |