I suggest you ...

Add ToQueue<T> and ToHashSet<T> extension methods on IEnumerable<T>

IEnumerable<T> has the ToList<T> extension method, and it would be very useful to have equivalent ToQueue<T> and ToHashSet<T> methods. Although Queue<T> and HashSet<T> have constructors that take an IEnumerable<T>, they cannot be used when T is an anonymous type. For example this is an error:

var q = new Queue(Enumerable.Range(1, 3).Select(i => new { Prop = 7 }));

But the equivalent using ToQueue works fine. It is fairly easy to define your own extension methods along the lines of:

public static HashSet<T> ToHashSet<T>(this IEnumerable<T> collection)
{
return new HashSet<T>(collection);
}

But the use case is very common, so they seem worth adding to the .NET Framework, and they would round out the set started with ToList<T>.

68 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Nick North shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

Thanks for taking the time to share this suggestion. This item has been around for a couple of versions of Visual Studio and we haven’t acted on it. Looking at the VS “15” plans, we’re not going to take action on this item, so we’re going to close it. If the suggestion is still relevant, please either take a look to see if there’s another suggestion that’s similar that you can vote on, or open a new suggestion.
- The Visual Studio Team

2 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • Anonymous commented  ·   ·  Flag as inappropriate

    While this could be part of the class library, there is a quite easy work around: simply provide your own extensions. BTDT, including other Methods like ToSortedList().
    Once it becomes part of the System.Linq namespace you just have to remove your custom extension methods to achieve source code compatibility.

    In fact you could imagine dozens of extensions like this, further polluting the IEnumerable<T> scope. I am unsure whether this is really useful to everyone. So no vote, sorry.

Feedback and Knowledge Base