在.NET库中是否有一个函数可以返回true或false来判断数组是空的还是空的?(类似于string.IsNullOrEmpty
)。
我在Array
类中查找了类似这样的函数,但什么也看不到。
即
var a = new string[]{};
string[] b = null;
var c = new string[]{"hello"};
IsNullOrEmpty(a); //returns true
IsNullOrEmpty(b); //returns true
IsNullOrEmpty(c); //returns false
发布于 2011-12-19 18:43:05
没有现有的扩展方法,但您可以使用此扩展方法:
/// <summary>Indicates whether the specified array is null or has a length of zero.</summary>
/// <param name="array">The array to test.</param>
/// <returns>true if the array parameter is null or has a length of zero; otherwise, false.</returns>
public static bool IsNullOrEmpty(this Array array)
{
return (array == null || array.Length == 0);
}
只需将其放在某个扩展类中,它就会扩展Array
,使其具有IsNullOrEmpty
方法。
发布于 2011-12-19 18:43:50
您可以创建自己的扩展方法:
public static bool IsNullOrEmpty<T>(this T[] array)
{
return array == null || array.Length == 0;
}
发布于 2014-03-14 21:12:51
如果使用ICollection<T>
,则更加通用
public static bool IsNullOrEmpty<T> (this ICollection<T> collection)
{
return collection == null || collection.Count == 0;
}
https://stackoverflow.com/questions/8560106
复制相似问题