我有这样的对象结构:
List.data[0].Contact.Mobile.MobileNumber
我需要检查 MobileNumber
是否为空。
问题是 Contact and Mobile
也可以为 null。
有没有比这样更好/更简单/更干净的方法:
(List.data[0].Contact!=null && List.data[0].Contact.Mobile!=null && List.data[0].Mobile.MobileNumber!=null)
?
最佳答案
在 C#6 中你可以试试这个
var exists = List.data[0].Contact?.Mobile?.MobileNumber != null;
关于c# - 检查元素是否存在或为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34606177/