Squ*_*ama 52 c# linq list c#-4.0
所以我知道这Find()只是一种List<T>方法,而First()对任何方法都是一种扩展IEnumerable<T>.我也知道,First()如果没有传递参数,将返回第一个元素,而Find()抛出异常.最后,我知道First()如果找不到元素会抛出异常,而Find()返回类型的默认值.
我希望能够解决我实际要问的问题.这是一个计算机科学问题,并在计算层面处理这些方法.我已经明白,IEnumerable<T>扩展并不总是像人们期望的那样运行.所以这是Q,我的意思是从"接近金属"的角度来看:Find()和之间的区别是什么First()?
这里有一些代码可以为这个问题提供基本的假设.
var l = new List<int> { 1, 2, 3, 4, 5 };
var x = l.First(i => i == 3);
var y = l.Find(i => i == 3);
Run Code Online (Sandbox Code Playgroud)
在上面的代码中如何First()和Find()发现它们的值之间是否有任何实际的计算差异?
注:让我们忽略的东西AsParallel()和AsQueryable()现在.
Tho*_*que 56
这是List<T>.Find(来自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);
}
Run Code Online (Sandbox Code Playgroud)
这是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();
}
Run Code Online (Sandbox Code Playgroud)
因此,两种方法的工作方式大致相同:它们迭代所有项目,直到找到与谓词匹配的项目.唯一明显的区别是Find使用for循环,因为它已经知道元素的数量,并First使用foreach循环,因为它不知道它.
BTW Find等于而FirstOrDefault()不是First()。因为如果谓词of First()不满足任何列表元素,那么您将获得异常。这就是返回dotpeek的地方,这是ReSharper的一些功能的另一个免费的免费反射器替代
这里的Enumerable.First(...)和Enumerable.FirstOrDefault(...)扩展方法:
public static TSource FirstOrDefault<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 element in source) {
if (predicate(element)) return element;
}
return default(TSource);
}
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 element in source) {
if (predicate(element)) return element;
}
throw Error.NoMatch();
}
Run Code Online (Sandbox Code Playgroud)
这是用于List <>。查找:
/// <summary>
/// Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire <see cref="T:System.Collections.Generic.List`1"/>.
/// </summary>
///
/// <returns>
/// The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type <paramref name="T"/>.
/// </returns>
/// <param name="match">The <see cref="T:System.Predicate`1"/> delegate that defines the conditions of the element to search for.</param><exception cref="T:System.ArgumentNullException"><paramref name="match"/> is null.</exception>
[__DynamicallyInvokable]
public T Find(Predicate<T> match)
{
if (match == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
for (int index = 0; index < this._size; ++index)
{
if (match(this._items[index]))
return this._items[index];
}
return default (T);
}
Run Code Online (Sandbox Code Playgroud)