是否有链式NULL检查之类的东西? [英] Is there such a thing as a chained NULL check?
本文介绍了是否有链式NULL检查之类的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下丑陋的代码:
if (msg == null ||
msg.Content == null ||
msg.Content.AccountMarketMessage == null ||
msg.Content.AccountMarketMessage.Account == null ||
msg.Content.AccountMarketMessage.Account.sObject == null) return;
是否有一种方法可以在C#中链式检查空值,因此我不必
Is there a way to chain check for null values in C#, so that I don't have to check each individual level?
推荐答案
(希望如此)您可以这样写:
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.
这篇关于是否有链式NULL检查之类的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文