什么是“if(谓词(项目,索引))”? LINQ 中的操作?

发布于 2024-10-25 03:11:23 字数 772 浏览 1 评论 0原文

我对 LINQ 相当陌生。我正在查看这段代码,不确定我是否正确理解了这一点。我意识到它是一个扩展和通用方法,但是谓词(item,index)执行什么(假设我在调用此方法时传入一个整数数组)?

我知道谓词是委托,但也许我只是不知道委托是如何工作的,有人有他们想要提供的任何好的示例/解释。另外,什么是 yield 关键字,它只是在 linq 中使用吗?

private static IEnumerable<TSource> WhereImpl<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, int, bool> predicate)
{
    int index = 0;
    foreach (TSource item in source)
    {
        if (predicate(item, index))
        {
            yield return item;
        }
        index++;
    }
}

我正在尝试遵循 重新实现 LINQ to Objects:第 2 部分 - “Where”,来自 Skeet 的博客。

I am fairly new to LINQ. I am looking at this code, and not sure if I understand this properly. I realize that it is an extension and generic method, but what is predicate(item, index) performing (lets say i pass in an array of ints when calling this method)?

I know that predicate is a delegate, but maybe I just don't know how delegation works, someone has any good example/explanation they'd like to give. Also, what is yield keyword, is it just used in linq stuff?

private static IEnumerable<TSource> WhereImpl<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, int, bool> predicate)
{
    int index = 0;
    foreach (TSource item in source)
    {
        if (predicate(item, index))
        {
            yield return item;
        }
        index++;
    }
}

I am trying to follow Reimplementing LINQ to Objects: Part 2 - "Where" from Skeet's blog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尾戒 2024-11-01 03:11:23
predicate(item, index)

被定义为一种类型

Func<TSource, int, bool>

,该类型意味着具有 TSourceint 参数并返回 bool - 谓词的方法。

TSource = string 的示例可能是(完全虚构):

bool IsLengthLargerThan(string s, int length)
{
  return s.Length > length;
}

还有,yield关键字是什么,是吗?
刚刚用在 linq 的东西上?

yield 特定于迭代器块 - 这在 LINQ 之前就已经存在了。它基本上像状态机一样工作 - yield return item; 将向调用者返回项目并暂停执行,但是一旦您请求下一个项目,执行将在下一行恢复。如果使用调试器单步调试,最容易了解它是如何工作的。

predicate(item, index)

is defined to be of type

Func<TSource, int, bool>

that means a method that has parameters of TSource and int and returns a bool - a predicate.

An example for TSource = string could be (totally made up):

bool IsLengthLargerThan(string s, int length)
{
  return s.Length > length;
}

Also, what is yield keyword, is it
just used in linq stuff?

yield is specific to iterator blocks - this has been around before LINQ. It basically works like a state machine - yield return item; will return item to the caller and suspend execution, but once you request the next item, execution will resume on the next line. It's easiest to see how it works if you step through it with a debugger.

棒棒糖 2024-11-01 03:11:23

首先,谓词(item,index)是一个委托,它接受枚举中的项目以及枚举中该项目的索引。因此,如果您从整数数组开始,则该项目将是一个整数,而索引将是其在数组中的索引。该索引是当前枚举实例的结果,因此如果您添加一个Where子句,并且某个项目的原始索引为3,并且where过滤掉了前3个,那么它的新索引将为0。

yield 关键字是一个 C# 关键字,用于轻松输出 IEnumerable 。

First the predicate(item, index) is a delegate that takes in the item in the enumeration and the index of that item in the enumeration. So if you started with an array of integers, the item would be an integer and the index would be its index in the array. This index is the result of the current instance of the enumeration so if you add a Where clause and the original index of an item was 3 and the where filtered out the first 3 then its new index would be 0.

The yield keyword is a C# keyword for easy output of an IEnumerable.

春风十里 2024-11-01 03:11:23

predicate(..) 是一个函数,它接受 2 个参数(一个字符串和一个 int),并返回 true 或 false。 yield return 是一个关键字,本质上就像在说“是的,将其添加到我将返回的 IEnumerable 中,但让我们继续寻找其他”,

因此它执行 谓词 带有字符串和整数的函数。您会看到索引被初始化为 0 并且每次都会自动递增。这是谓词的第二个参数。当函数返回 true 时,您说“将其添加到我的返回集合中”

predicate(..) is a function that takes 2 parameters, a string and an int, and return true or false. yield return is a keyword that essentially is like saying "yea, add this to the IEnumerable that I'll be returning, but let's keep looking for others"

So it executes the predicate function with a string and an int. You see index was initialized to 0 and is auto-incremented every time. That's the second param to predicate. When the function returns true, you say "add it to my return collection"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文