假设我有这样的函数:
public List<int> func()
{}
Run Code Online (Sandbox Code Playgroud)
现在,出于某种原因,我没有任何东西可以放入列表中,所以我将返回"空的东西".根据一个好的做法,我应该在上面的函数中返回以下三个中的哪一个?
return null;
return new List<int>();
return default(List<int>); //Is this the same thing as the one above?
Run Code Online (Sandbox Code Playgroud)
返回一个空列表:
return new List<int>();
Run Code Online (Sandbox Code Playgroud)
该default(List<int>)会返回null; 不是空名单!
返回空列表可防止在每次调用函数后进行过多的空值检查.请参阅返回null或空集合是否更好?
| 归档时间: |
|
| 查看次数: |
86 次 |
| 最近记录: |