Rationale
When dealing with collections it isn't uncommon that one needs to be able to get the count of an IEnumerable<T> as a default value for a new collection's capacity. The naive thing to do would be to simply use the Enumerable.Count extension method however if the IEnumerable<T> isn't an ICollection<T> or any of the other interfaces or types with a Count or Length property that method results in enumerating the collection which isn't desirable when just wanting a default value for a new collection's capacity. Instead of manually checking for those various interfaces each time someone needs this functionality I would like a non-enumerating count linq method added.
Proposed API
namespace System.Linq {
public static class Enumerable {
+ public static bool TryGetNonEnumeratedCount(IEnumerable<T> source, out int count);
}
}
Details
It would essentially retrieve the count so long as it doesn't necessitate enumerating the collection.
This would be a somewhat fragile method in that it should be updated whenever the next interface or type with a Count or Length property is introduced and thus may exhibit different behavior between versions.
Use Cases
It could be used in collection constructors where initializing a collection with an IEnumerable<T> is common and a good initial capacity is important to reduce resizing costs such as in the Dictionary<TKey, TValue> constructor here or the HashSet<T> constructor here. Unfortunately, since this method will be implemented at a higher level than these corelib types we won't be able to use it there.
Updates
- Switched to
Count with an allowEnumeration boolean parameter.
- Switched to
TryGetCount.
- Switched to
TryGetNonEnumeratedCount.
Rationale
When dealing with collections it isn't uncommon that one needs to be able to get the count of an
IEnumerable<T>as a default value for a new collection's capacity. The naive thing to do would be to simply use theEnumerable.Countextension method however if theIEnumerable<T>isn't anICollection<T>or any of the other interfaces or types with aCountorLengthproperty that method results in enumerating the collection which isn't desirable when just wanting a default value for a new collection's capacity. Instead of manually checking for those various interfaces each time someone needs this functionality I would like a non-enumerating count linq method added.Proposed API
namespace System.Linq { public static class Enumerable { + public static bool TryGetNonEnumeratedCount(IEnumerable<T> source, out int count); } }Details
It would essentially retrieve the count so long as it doesn't necessitate enumerating the collection.
This would be a somewhat fragile method in that it should be updated whenever the next interface or type with a
CountorLengthproperty is introduced and thus may exhibit different behavior between versions.Use Cases
It could be used in collection constructors where initializing a collection with an
IEnumerable<T>is common and a good initial capacity is important to reduce resizing costs such as in theDictionary<TKey, TValue>constructor here or theHashSet<T>constructor here. Unfortunately, since this method will be implemented at a higher level than these corelib types we won't be able to use it there.Updates
Countwith anallowEnumerationboolean parameter.TryGetCount.TryGetNonEnumeratedCount.