且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将代码转换为.net 4.5

更新时间:2023-02-16 23:19:25

两个选项。

首先使用条件表达式执行相同的条件运算符行为:

First use a conditional expression doing the same conditional operator behavior:

return this.Children != null ? this.Children.Count(f=>f!=null) > 0 : false;

其次,保护您的Children属性不受null:

Second, protect your Children property against the null:

return this.Children.Count(f => f != null) > 0;

public ObservableCollection<DirectoryItemViewModel> Children {
 get
{
  return _Children ?? Enumerable.Empty<DirectoryItemViewModel>();
}
            
set
{
  _Children = value;                
}
}

Personaly,我更喜欢第二种,因为每次我需要Children属性时,我都不需要保护我的代码免受null 。

Personaly, I prefer the second because each time I need the Children property I don't need to protect my code against the null.

问候,