tags:

views:

2805

answers:

6

What is a good introduction or tutorial article for learning how to use list comprehensions in C#?

+1  A: 

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;
}
Ian P
Unfortunately, I was not the one to vote you down :-\
Nescio
+2  A: 

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

List Comprehension in C# with LINQ

List Comprehension in C# with LINQ - Part 2

muloh
+2  A: 

@Ian P

 return (from user in users
         where user.Valid
         select user.Name).ToArray();
Nescio
He didn't specify LINQ, so I just went as basic as possible. Your example is definitely preferred.
Ian P
A: 

Here is a CodeProject article describing list comprehensions in C# 2.0

Mike Thompson
A: 

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

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

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

the Python way is like this: list = [2*number for i in range(0,10)]

and in C#: var list2 = from number in 0.To(10) select 2*number;

Both methods are lazily evaluated.

Justin Bozonier
+8  A: 

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).

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:

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:

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.Range to the Linq expression that generates the evens sequence. In the C# List Comprehension I am reducing the set numbers by applying a function (the modulo 2) to that sequence. This produces the evens sequence 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.

The full code listing is available here.

Jonas Gorauskas