Lou*_*hys 0 .net c# collections
String有一个方便的String.IsNullOrEmpty方法,用于检查字符串是否为null或长度为零.在开箱即用的.net中有类似的东西吗?
Hab*_*bib 17
没有,但我认为你可以为此编写自己的扩展方法.
public static bool IsNullOrEmpty(this ICollection collection)
{
if (collection == null)
return true;
return collection.Count < 1;
}
Run Code Online (Sandbox Code Playgroud)
这是一个更通用的扩展方法,可以在任何IEnumerable上使用.
public static bool IsNullOrEmpty(this IEnumerable collection)
{
return collection == null || !collection.Cast<object>().Any();
}
Run Code Online (Sandbox Code Playgroud)
如果某些东西是空的,我不是那些返回true的函数的忠实粉丝,我总是发现大多数时候我需要添加一个!到string.IsNullOrEmptyString的前面.我会把它写成"ExistsAndHasItems"或类似的东西.
| 归档时间: |
|
| 查看次数: |
1910 次 |
| 最近记录: |