Is there such a thing as a chained NULL check?

前端 未结 7 458
灰色年华
灰色年华 2021-01-18 05:30

I have the following ugly code:

if (msg == null || 
    msg.Content == null || 
    msg.Content.AccountMarketMessage == null || 
    msg.Content.AccountMarke         


        
7条回答
  •  攒了一身酷
    2021-01-18 06:21

    One of the proposals in C# 6 would be to add a new Null Propogation operator.

    This will (hopefully) allow you to write:

    var obj = msg?.Content?.AccountMarketMessage?.Account?.sObject;
    if (obj == null) return;
    

    Unfortunately, there is nothing in the language at this point that handles this.

提交回复
热议问题