Regardless of the collection type I use as input, LINQ always returns IEnumerable<MyType> instead of List<MyType> or HashSet<MyType>.
From the MSDN tutorial:
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
// numQuery is an IEnumerable<int> <====== Why IEnumerable<int> instead of int[]?
var numQuery =
from num in numbers
where (num % 2) == 0
select num;
I wonder what’s the rationale behind the decision to not preserve the collection type (like the element type), not the suggested work-around.
I know that toArray, toDictionary or toList exist, that’s not the question.
Edit: How does the implementation in C# differ from Scala where it works without overriding the return types everywhere?
Short answer: C#’s type system is too primitive to support preservation of collection type in higher order functions (without code duplication, that is).
Long answer: Refer to my post here. (Actually it’s about Java, but applies to C# as well.)