将代码转换为.net 4.5 [英] Convert Code to .net 4.5

查看:87
本文介绍了将代码转换为.net 4.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个空条件?运营商。我们如何将其转换回4.5代码

Having this null conditional ?. operator. How can we convert it back to 4.5 code

//How to convert this Function to .net 4.5 version..Code was shortend for clarity

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

public ObservableCollection<DirectoryItemViewModel> Children {
 get
{
  return _Children;
}
            
set
{
  _Children = value;                
}
}




推荐答案

两个选项。

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

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.

问候,


这篇关于将代码转换为.net 4.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆