首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ICollection / ICollection<T>模糊问题

ICollection / ICollection<T>模糊问题
EN

Stack Overflow用户
提问于 2009-10-09 15:24:35
回答 2查看 2.2K关注 0票数 4

只想对句法系统进行简单的扩展:

代码语言:javascript
复制
public static bool IsNotEmpty(this ICollection obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}

public static bool IsNotEmpty<T>(this ICollection<T> obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}

当我处理一些集合时,它会很好地工作,但是当我与其他集合一起工作时,我会得到

调用在以下方法或属性之间是不明确的:'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.IList)‘和'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.Generic.ICollection)’

这个问题有规范的解决方案吗?

不,我不想在调用此方法之前执行强制转换;)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-10-12 10:43:23

解决歧义的最佳方法是:为所有常见的非泛型ICollection类定义重载。这意味着自定义ICollection将不兼容,但这不是什么大不了的,因为泛型正在成为常态。

以下是整个代码:

代码语言:javascript
复制
/// <summary>
/// Check the given array is empty or not
/// </summary>
public static bool IsNotEmpty(this Array obj)
{
    return ((obj != null)
        && (obj.Length > 0));
}
/// <summary>
/// Check the given ArrayList is empty or not
/// </summary>
public static bool IsNotEmpty(this ArrayList obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}
/// <summary>
/// Check the given BitArray is empty or not
/// </summary>
public static bool IsNotEmpty(this BitArray obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}
/// <summary>
/// Check the given CollectionBase is empty or not
/// </summary>
public static bool IsNotEmpty(this CollectionBase obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}
/// <summary>
/// Check the given DictionaryBase is empty or not
/// </summary>
public static bool IsNotEmpty(this DictionaryBase obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}
/// <summary>
/// Check the given Hashtable is empty or not
/// </summary>
public static bool IsNotEmpty(this Hashtable obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}
/// <summary>
/// Check the given Queue is empty or not
/// </summary>
public static bool IsNotEmpty(this Queue obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}
/// <summary>
/// Check the given ReadOnlyCollectionBase is empty or not
/// </summary>
public static bool IsNotEmpty(this ReadOnlyCollectionBase obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}
/// <summary>
/// Check the given SortedList is empty or not
/// </summary>
public static bool IsNotEmpty(this SortedList obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}
/// <summary>
/// Check the given Stack is empty or not
/// </summary>
public static bool IsNotEmpty(this Stack obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}
/// <summary>
/// Check the given generic is empty or not
/// </summary>
public static bool IsNotEmpty<T>(this ICollection<T> obj)
{
    return ((obj != null)
        && (obj.Count > 0));
}

请注意,我不希望它在IEnumerable<T>上工作,因为Count()是一种可以触发数据库请求的方法,如果您正在使用Linq或Linq。

票数 4
EN

Stack Overflow用户

发布于 2009-10-09 15:35:10

因为有些集合实现了这两个接口,所以应该将集合转换为如下所示的具体接口

代码语言:javascript
复制
((ICollection)myList).IsNotEmpty();

代码语言:javascript
复制
((ICollection<int>)myIntList).IsNotEmpty();

是的,如果obj ==为null,您将得到==,因此您可以删除null检查;)这意味着您的扩展方法只是将计数与0进行比较,而不使用扩展方法;)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1544480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档