为什么 Func<> 的这个 MSDN 示例是这样的?委托有多余的 Select() 调用吗?

发布于 2024-08-30 01:16:43 字数 873 浏览 4 评论 0 原文

MSDN 在 Func 通用委托

Func<String, int, bool> predicate = ( str, index) => str.Length == index;

String[] words = { "orange", "apple", "Article", "elephant", "star", "and" };
IEnumerable<String> aWords = words.Where(predicate).Select(str => str);

foreach (String word in aWords)
    Console.WriteLine(word);

我了解所有内容这是在做。我不明白的是

Select(str => str)

一点。这肯定不需要吧?如果你把它省略掉,

IEnumerable<String> aWords = words.Where(predicate);

然后你仍然会得到一个包含相同结果的 IEnumerable 返回,并且代码打印相同的内容。

我错过了什么,或者这个例子有误导性吗?

The MSDN gives this code example in the article on the Func Generic Delegate:

Func<String, int, bool> predicate = ( str, index) => str.Length == index;

String[] words = { "orange", "apple", "Article", "elephant", "star", "and" };
IEnumerable<String> aWords = words.Where(predicate).Select(str => str);

foreach (String word in aWords)
    Console.WriteLine(word);

I understand what all this is doing. What I don't understand is the

Select(str => str)

bit. Surely that's not needed? If you leave it out and just have

IEnumerable<String> aWords = words.Where(predicate);

then you still get an IEnumerable back that contains the same results, and the code prints the same thing.

Am I missing something, or is the example misleading?

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

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

发布评论

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

评论(4

娇纵 2024-09-06 01:16:43

Select 确实是多余的。

我怀疑这个例子可能是从查询理解语法“翻译”而来的,如下所示:

IEnumerable<String> aWords = 
    from w in words
    where (...)
    select w;

当使用此语法时,您必须在最后选择,这就是如何编译器工作了。但是,当使用 Where 扩展方法时,完全没有必要,除非您确实需要进行单独的投影。

或者,也许这只是一个错误。 MSDN 作者并非绝对正确!

The Select is indeed redundant.

I suspect that this example may have been "translated" from the query comprehension syntax, as in:

IEnumerable<String> aWords = 
    from w in words
    where (...)
    select w;

When using this syntax, you have to select at the end, it's just how the compiler works. When using the Where extension method, however, it's completely unnecessary unless you actually need to do a separate projection.

Or, maybe it's just a mistake. The MSDN writers aren't infallible!

自演自醉 2024-09-06 01:16:43

不,不需要。

如果您想强制延迟评估序列(即防止强制转换),则可以使用此类构造。如果您有一个返回 List 的方法,但声明了 IEnumerable 返回类型,则客户端可以强制转换返回类型并直接操作底层列表。显然这是一个非常糟糕的主意,但是类可以通过应用身份选择来保护其状态,例如本示例中使用的选择:

public IEnumerable<T> Items
{
   get { return privateList.Select(i => i); }
}

No it's not needed.

Such a construct could be used if you wanted to force a sequence to be lazily evaluated i.e. to prevent casting. If you had a method that returned a List<T> but declared an IEnumerable<T> return type then a client could cast the return type and manipulate the underlying list directly. Obviously this is a very bad idea, but a class could protect its state by applying an identity select such as the one used in this example:

public IEnumerable<T> Items
{
   get { return privateList.Select(i => i); }
}
貪欢 2024-09-06 01:16:43

事实上,这是不需要的,msdn 页面。告诉他们不需要 .Select。 Select 子句的事实只是:

Select(str => str)

它接收字符串并输出相同字符串

Indeed, it's not needed, there's a feedback button on bottom of msdn page. Tell them the .Select is not needed. The mere fact that the Select clause is just:

Select(str => str)

It receives string and outputs the same string

执妄 2024-09-06 01:16:43

你那里有一个非常奇怪的链接。该主题不在目录中,并且顶部有以下内容:

“[该主题是预发行文档,在未来版本中可能会发生更改。空白主题作为占位符包含在内。]”

第一行看起来也像是作者对自己的评论。

而且自从 VS 2010 和 .NET 4.0 刚刚发布以来,我想,这是某种没有及时删除/替换的损坏主题。

我认为,现在此内容的正确 URL 是这样的: http://msdn .microsoft.com/en-us/library/bb534303.aspx

顺便问一下,您是如何获取 URL 的?是通过MSDN搜索还是其他什么方式?

You have a very strange link there. This topic is not in TOC, and it has the following at the top:

"[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]"

The first line also looks like writer's comment to himself/herself.

And since VS 2010 and .NET 4.0 just released, I guess, this is some kind of broken topic that hasn't been deleted/replaced in time.

I think, the correct URL now for this content is this: http://msdn.microsoft.com/en-us/library/bb534303.aspx

By the way, how did you get your URL? Was it through MSDN search or something else?

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