Just a little niggle about LINQ syntax. I\'m flattening an IEnumerable
with SelectMany(x => x)
.
My problem i
Note: this answer was correct for C# 3, but at some point (C# 4? C# 5?) type inference improved so that the IdentityFunction
method shown below can be used easily.
No, there isn't. It would have to be generic, to start with:
public static Func IdentityFunction()
{
return x => x;
}
But then type inference wouldn't work, so you'd have to do:
SelectMany(Helpers.IdentityFunction())
which is a lot uglier than x => x
.
Another possibility is that you wrap this in an extension method:
public static IEnumerable Flatten
(this IEnumerable> source)
{
return source.SelectMany(x => x);
}
Unfortunately with generic variance the way it is, that may well fall foul of various cases in C# 3... it wouldn't be applicable to List
for example. You could make it more generic:>
public static IEnumerable Flatten
(this IEnumerable source) where TWrapper : IEnumerable
{
return source.SelectMany(x => x);
}
But again, you've then got type inference problems, I suspect...
EDIT: To respond to the comments... yes, C# 4 makes this easier. Or rather, it makes the first Flatten
method more useful than it is in C# 3. Here's an example which works in C# 4, but doesn't work in C# 3 because the compiler can't convert from List
to >
IEnumerable
:
using System;
using System.Collections.Generic;
using System.Linq;
public static class Extensions
{
public static IEnumerable Flatten
(this IEnumerable> source)
{
return source.SelectMany(x => x);
}
}
class Test
{
static void Main()
{
List> strings = new List>
{
new List { "x", "y", "z" },
new List { "0", "1", "2" }
};
foreach (string x in strings.Flatten())
{
Console.WriteLine(x);
}
}
}