小编urv*_*ish的帖子

对象null和空检查的最佳方法是什么?

我在C#中有如下代码:

If  (this._university != Null and this._university.department !=null &&
     this._university.department.class !=null && 
     this._university.department.class.student != null && 
     this._university.department.class.student.name != null &&
     this._university.department.class.student.name.firstname != null &&
     this._university.department.class.student.name.firstname !=String.empty)

{
   // selecting first letter of Firstname
   var F_firstname = this._university.department.class.student.name.firstname[0].tostring();
}
Run Code Online (Sandbox Code Playgroud)

但是代码看起来非常糟糕,无法检查null对象.我们有更好的方法来检查对象的空值吗?

c#

0
推荐指数
1
解决办法
1206
查看次数

标签 统计

c# ×1