Runtime: GetConsumingEnumerable<T>

Created on 5 Nov 2020  路  6Comments  路  Source: dotnet/runtime

It would be great if Stack<T> and Queue<T> had a GetConsumingEnumerable<T> extension method so I could do things similar to the following:

var First5 = MyQueue.GetConsumingEnumerable().Take(5).ToList();
api-suggestion area-System.Collections

Most helpful comment

Not saying "no" :smile: but I think it'd need a bunch of folks citing its importance before we did. It's easily done on ones own if they need it (as shown above), with I think little benefit to it actually being part of Queue<T> (i.e. I'm not sure there's anything we could do significantly better in an implementation on the type itself).

All 6 comments

Tagging subscribers to this area: @eiriktsarpalis, @jeffhandley
See info in area-owners.md if you want to be subscribed.

You mean something like:
C# public static IEnumerable<T> GetConsumingEnumerable<T>(this Queue<T> queue) { while (queue.TryDequeue(out T item)) yield return item; }
?

@stephentoub Yep! Can you add that to the framework?

Not saying "no" :smile: but I think it'd need a bunch of folks citing its importance before we did. It's easily done on ones own if they need it (as shown above), with I think little benefit to it actually being part of Queue<T> (i.e. I'm not sure there's anything we could do significantly better in an implementation on the type itself).

Tagging subscribers to this area: @eiriktsarpalis, @jeffhandley
See info in area-owners.md if you want to be subscribed.

Given that this can be trivially defined as an extension method, I don't think we would add this unless there was clear demand from many users.

Was this page helpful?
0 / 5 - 0 ratings