Is there such a thing as a chained NULL check?

前端 未结 7 441
灰色年华
灰色年华 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:31

    There is not currently such a thing, but it may be coming to .NET very soon. There is a well-known User Voice thread on the subject. And as noted in this article, the Visual Studio team has recently announced that:

    We are seriously considering this feature for C# and VB, and will be prototyping it in coming months.

    Edit: and as noted in Reed Copsey's answer above, it is now a planned addition for C# 6. There are better details on the Codeplex pages he linked.

    0 讨论(0)
提交回复
热议问题