/// <summary>

/// <returns>true if the collection has a length and contains only non-null elements.</returns>
public static bool HasElements(ICollection collection)
{
if (!HasLength(collection)) return false;
IEnumerator it = collection.GetEnumerator();
while (it.MoveNext())
{
if (it.Current == null) return false;
}
return true;
}

/// <summary>
/// Checks if the given array or collection is null or has no elements.
/// </summary>
/// <param name="collection"></param>
/// <returns></returns>
public static bool HasLength(ICollection collection)
{
return !((collection == null) || (collection.Count == 0));
}

 备注:GetEnumerator是返回实例的枚举数。换句话说就是将集合中的所有元素一个一个列出来。我们可以通过MoveNext()得到集合中的 所有元素

相关文章:

  • 2022-01-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
  • 2021-08-25
相关资源
相似解决方案