C# 列表推导式简介

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/130898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:55:15  来源:igfitidea点击:

Introduction to C# list comprehensions

提问by William Hutchen

How can I perform list comprehensions in C#?

如何在 C# 中执行列表推导式?

回答by Ian P

While this isn't a tutorial, here's some code that illustrates the concept:

虽然这不是教程,但这里有一些代码来说明这个概念:

public List<string> ValidUsers(List<User> users) {
  List<string> names = new List<string>();
  foreach(User user in users) {
    if(user.Valid) {
      names.Add(user.Name);
    }
  }
  return names;
}

回答by brock.holum

You can use LINQ to make expressions that are similar to list comprehensions. Here's a site explaining it a little:

您可以使用 LINQ 来创建类似于列表推导式的表达式。这是一个解释它的网站:

List Comprehension in C# with LINQ

使用 LINQ 在 C# 中进行列表推导

List Comprehension in C# with LINQ - Part 2

使用 LINQ 在 C# 中进行列表推导 - 第 2 部分

回答by Nescio

@Ian P

@伊恩P

 return (from user in users
         where user.Valid
         select user.Name).ToArray();

回答by Justin Bozonier

Found this when I was looking up how to do list comprehensions in C#...

当我在查找如何在 C# 中执行列表推导式时发现了这个...

When someone says list comprehensions I immediately think about Python. The below code generates a list that looks like this:

当有人说列表推导式时,我立即想到了 Python。下面的代码生成一个如下所示的列表:

[0,2,4,6,8,10,12,14,16,18]

The Python way is like this:

Python的方式是这样的:

list = [2*number for number in range(0,10)]

In C#:

在 C# 中:

var list2 = from number in Enumerable.Range(0, 10) select 2*number;

Both methods are lazily evaluated.

这两种方法都是惰性评估的。

回答by λ Jonas Gorauskas

A List Comprehension is a type of set notation in which the programmer can describe the properties that the members of a set must meet. It is usually used to create a set based on other, already existing, set or sets by applying some type of combination, transform or reduction function to the existing set(s).

List Comprehension 是一种集合表示法,程序员可以在其中描述集合成员必须满足的属性。它通常用于通过将某种类型的组合、变换或归约函数应用于现有集合来创建基于其他、已经存在的一个或多个集合的集合。

Consider the following problem: You have a sequence of 10 numbers from 0 to 9 and you need to extract all the even numbers from that sequence. In a language such a C# version 1.1, you were pretty much confined to the following code to solve this problem:

考虑以下问题:您有一个从 0 到 9 的 10 个数字的序列,您需要从该序列中提取所有偶数。在诸如 C# 1.1 版这样的语言中,您几乎只能使用以下代码来解决此问题:

ArrayList evens = new ArrayList();
ArrayList numbers = Range(10);
int size = numbers.Count;
int i = 0;

while (i < size) 
{
    if (i % 2 == 0) 
    {
        evens.Add(i);
    }
    i++;
}

The code above does not show the implementation of the Range function, which is available in the full code listing below. With the advent of C# 3.0 and the .NET Framework 3.5, a List Comprehension notation based on Linq is now available to C# programmers. The above C# 1.1 code can be ported to C# 3.0 like so:

上面的代码没有显示 Range 函数的实现,它可以在下面的完整代码列表中找到。随着 C# 3.0 和 .NET Framework 3.5 的出现,C# 程序员现在可以使用基于 Linq 的 List Comprehension 表示法。上面的 C# 1.1 代码可以像这样移植到 C# 3.0:

IEnumerable<int> numbers = Enumerable.Range(0, 10);
var evens = from num in numbers where num % 2 == 0 select num;

And technically speaking, the C# 3.0 code above could be written as a one-liner by moving the call to Enumarable.Rangeto the Linq expression that generates the evenssequence. In the C# List Comprehension I am reducing the set numbersby applying a function (the modulo 2) to that sequence. This produces the evenssequence in a much more concise manner and avoid the use of loop syntax. Now, you may ask yourself: Is this purely syntax sugar? I don't know, but I will definitelly investigate, and maybe even ask the question myself here. I suspect that this is not just syntax sugar and that there are some true optimizations that can be done by utilizing the underlying monads.

和技术上来说,在C#3.0以上代码可以通过移动呼叫要被写入作为一衬垫Enumarable.Range到生成的LINQ表达式找齐序列。在 C# List Comprehension 中,我通过将函数(模 2)应用于该序列来减少集合。这会以更简洁的方式生成evens序列,并避免使用循环语法。现在,您可能会问自己:这纯粹是语法糖吗?我不知道,但我一定会调查,甚至可能会在这里自己问这个问题。我怀疑这不仅仅是语法糖,还有一些真正的优化可以通过利用底层 monad 来完成。

The full code listing is available here.

完整的代码清单可在此处获得