C# First() 和 Find() 的区别 [英] C# Difference between First() and Find()

查看:20
本文介绍了C# First() 和 Find() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道 Find() 只是一个 List 方法,而 First() 是任何<代码>IEnumerable.我也知道 First() 会在没有传递参数时返回第一个元素,而 Find() 会抛出异常.最后,我知道如果未找到元素,First() 将抛出异常,而 Find() 将返回类型的默认值.

So I know that Find() is only a List<T> method, whereas First() is an extension for any IEnumerable<T>. I also know that First() will return the first element if no parameter is passed, whereas Find() will throw an exception. Lastly, I know that First() will throw an exception if the element is not found, whereas Find() will return the type's default value.

我希望这能消除我对实际询问内容的困惑.这是一个计算机科学问题,并在计算级别处理这些方法.我开始明白 IEnumerable 扩展并不总是像人们所期望的那样在幕后运作.所以这里是 Q,我的意思是从接近金属"的角度来看:Find()First() 之间有什么区别?

I hope that clears up confusion about what I'm actually asking. This is a computer science question and deals with these methods at the computational level. I've come to understand that IEnumerable<T> extensions do not always operate as one would expect under the hood. So here's the Q, and I mean from a "close to the metal" standpoint: What is the difference between Find() and First()?

以下是一些代码,用于提供在此问题下进行操作的基本假设.

Here's some code to provide basic assumptions to operate under for this question.

var l = new List<int> { 1, 2, 3, 4, 5 };
var x = l.First(i => i == 3);
var y = l.Find(i => i == 3);

First()Find() 如何在上面的代码中发现它们的值之间是否有任何实际的计算差异?

Is there any actual computational difference between how First() and Find() discover their values in the code above?

注意:让我们暂时忽略诸如 AsParallel()AsQueryable() 之类的东西.

Note: Let us ignore things like AsParallel() and AsQueryable() for now.

推荐答案

以下是 List.Find 的代码(来自 Reflector):

Here's the code for List<T>.Find (from Reflector):

public T Find(Predicate<T> match)
{
    if (match == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
    }
    for (int i = 0; i < this._size; i++)
    {
        if (match(this._items[i]))
        {
            return this._items[i];
        }
    }
    return default(T);
}

这里是Enumerable.First:

public static TSource First<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    if (predicate == null)
    {
        throw Error.ArgumentNull("predicate");
    }
    foreach (TSource local in source)
    {
        if (predicate(local))
        {
            return local;
        }
    }
    throw Error.NoMatch();
}

所以这两种方法的工作方式大致相同:它们迭代所有项目,直到找到与谓词匹配的项目.唯一明显的区别是 Find 使用 for 循环,因为它已经知道元素的数量,而 First 使用 foreach 循环,因为它不知道不知道.

So both methods work roughly the same way: they iterate all items until they find one that matches the predicate. The only noticeable difference is that Find uses a for loop because it already knows the number of elements, and First uses a foreach loop because it doesn't know it.

这篇关于C# First() 和 Find() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆