I want to generate a list in C#. I am missing python's list comprehensions. Is there a C# way to create collections on the fly like list comprehensions or generator expressions do in python?
相关问题
- how to define constructor for Python's new Nam
- Sorting 3 numbers without branching [closed]
- streaming md5sum of contents of a large remote tar
- Graphics.DrawImage() - Throws out of memory except
- How to get the background from multiple images by
I know this is very very late an answer, but I too wondered if C# has anything equivalent to python's list comprehension. Answers from both Matt Campbell and Jon Skeet show how to extract a list from another existing one, as far as I understood. But a python's list comprehension can also create a list from scratch. So here is what I came up with.
First I will show the python list comprehension, and then its C# equivalent that I came up with.
The toy task is to create a string like this
Python list comprehension:
C# equivalent I came up with:
I am not sure if I over-did anything or not though.
Edit: (from what Jon Skeet mentioned in his comment, a real one-liner equivalent to python's list comprehension)
Note that
$
thing is a C# 6 feature. If you are still not using C# 6, you can go with the oldString.Format()
way.If you are using C# 3.0 (VS2008) then LINQ to Objects can do very similar things:
List<T>.ConvertAll
behaves just like list comprehensions by performing the same operation on every item on an existing list and then returning a new collection. This is an alternative to using Linq especially if you are still using .NET 2.0.In Python, a simple list comprehension example:
For C# 3.0, you can pass a lambda function specifying what type of mapping function is needed.
For C# 2.0, you can use an anonymous method with the
Converter
delegate to perform the equivalent.(Note: the same can be done with Arrays using
Array.ConvertAll
There's this:
which is only a little longer than its python equivalent:
And then there is this:
which is the python equivalent of:
Matt has mentioned query expressions. These are available for LINQ in general, by the way - not just LINQ to Objects. (For example, the same query applied to a LINQ to SQL datacontext would execute the filter and projection on the database.)
The query expressions in C# 3 are simply syntactic sugar over writing normal C# code - although query expressions usually end up calling extension methods. (They don't have to, and the compiler doesn't care, but they usually do.) There are various things you can do with collections which aren't available in C# query expressions, but which are supported by method calls, so it's worth being aware of both kinds of syntax. For instance, Matt's query expression of:
is "pre-processed" into:
If you want to (say) filter based on the index of the value in the original collection, you can use an appropriate overload of Where which is unavailable via query expressions:
Or you could find the length of the longest name matching the criteria:
(You don't have to do the projection and max in separate methods - there's a
Max
overload which takes a projection as well.)My point is that using extension methods you can very easily build up sophisticated queries.
You mention Python generators as well - C# has this in the form of iterator blocks. Indeed, these are incredibly useful when implementing LINQ-like operators. (Because most of LINQ to Objects is based on extension methods, you can add your own operators which look "native" to LINQ - although you can't change the query expression syntax yourself.)