首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >IEnumerable可拓方法问题

IEnumerable可拓方法问题
EN

Stack Overflow用户
提问于 2013-09-18 12:10:04
回答 3查看 495关注 0票数 0

我有一个从因特网下载的IEnumerable扩展方法,我正在努力编写一个有效的回顾性测试。

可拓方法

代码语言:javascript
运行
复制
public static bool In<T>(this T source, params T[] list) where T : IEnumerable
{
    if (source == null) throw new ArgumentNullException("source");
    return list.Contains(source);
}

测试

代码语言:javascript
运行
复制
[TestMethod]
public void In()
{

    IEnumerable<int> search = new int[] { 0 };
    IEnumerable<int> found = new int[] { 0, 0, 1, 2, 3 };
    IEnumerable<int> notFound = new int[] { 1, 5, 6, 7, 8 };

    Assert.IsTrue(search.In(found));
    Assert.IsFalse(search.In(notFound));
}

结果

所有代码都会编译,但是在这两个断言中,当我认为第一个断言search.In(found)应该返回true时,扩展方法的结果返回false。

问题

我是在调用代码中做错了什么,还是扩展名出现了问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-18 12:13:17

要运行此测试,应该如下所示:

代码语言:javascript
运行
复制
[TestMethod]
public void In()
{

    IEnumerable<int> search = new int[] { 0 };
    IEnumerable<int> a = new int[] { 0, 0, 1, 2, 3 };
    IEnumerable<int> b = new int[] { 0, 0, 1, 2, 3 };
    IEnumerable<int> c = new int[] { 0};

    Assert.IsTrue(search.In(a,b,c));
    Assert.IsFalse(search.In(a,b));
}

这是因为您在这里搜索的是整组int,而不是其中的项目。

在上面的代码中,您正在检查在所有传递的参数中是否存在您正在搜索的数组。

如果您想要在列表中包含检查是否项的扩展名,请尝试如下:

代码语言:javascript
运行
复制
public static bool In<T> (this T value, IEnumerable<T> list)
{
     return list.Contains(value);
}

然后你就可以:

代码语言:javascript
运行
复制
[TestMethod]
public void In()
{

    IEnumerable<int> search = 0;
    IEnumerable<int> found = new int[] { 0, 0, 1, 2, 3 };
    IEnumerable<int> notFound = new int[] { 1, 5, 6, 7, 8 };

    Assert.IsTrue(search.In(found));
    Assert.IsFalse(search.In(notFound));
}

编辑:

谢谢您对Marcin的评论

票数 1
EN

Stack Overflow用户

发布于 2013-09-18 12:13:25

你的TIEnumerable<int>,而不是int。将局部变量键入为int[]。现在,您正在使用params参数槽中的单个IEnumerable<int>调用IEnumerable<int>

再看一看,你那里的分机是假的。为什么源项和搜索项( params参数)都应该是IEnumerable?没有任何意义。以下是一个工作版本:

代码语言:javascript
运行
复制
    public static bool In<T>(this T operand, params T[] values) where T : IEquatable<T>
    {
        if (values == null) throw new ArgumentNullException("operand");

        return In(operand, ((IEnumerable<T>)values));
    }
    public static bool In<T>(this T operand, IEnumerable<T> values) where T : IEquatable<T>
    {
        if (values == null) throw new ArgumentNullException("operand");

        return values.Contains(operand);
    }
票数 1
EN

Stack Overflow用户

发布于 2013-09-18 12:24:21

如果您想检查天气,source中的所有元素都在list

代码语言:javascript
运行
复制
    public static bool In<T>(this IEnumerable<T> source, IEnumerable<T> list)
    {
        if (source == null) throw new ArgumentNullException("source");
        return source.All(e => list.Contains(e));
    }

    IEnumerable<int> search = new int[] { 0 };
    IEnumerable<int> found = new int[] { 0, 0, 1, 2, 3 };
    IEnumerable<int> notFound = new int[] { 1, 5, 6, 7, 8 };

    Console.WriteLine(search.In(found));//True
    Console.WriteLine(search.In(notFound));//False
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18871905

复制
相关文章

相似问题

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