-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Non-enumerating Count Linq Method #27183
Comments
Alternatively we could add a |
Don't like this approach. The
There are plenty of examples today where there is a pair of methods If we were to add an API like this I tihnk it would need to be a pair of methods:
|
So you would prefer something like the following? namespace System.Linq {
public static class Enumerable {
+ public static int? NonEnumeratingCount(this IEnumerable<TSource> source);
+ public static int NonEnumeratingCountOrDefault(this IEnumerable<TSource> source, int defaultValue);
}
} I'm not sure an var capacity = source.NonEnumeratingCount() ?? 0; |
Instead of |
What's wrong with this?
The only purpose of |
Or bool TryCount<T>(this IEnumerable<T> source, out int count) 🤔 |
@Grauenwolf I guess my main motivation is to support I've created the new issue dotnet/corefx#35173 to address adding implicit support for |
Ad-hoc implementations that try to calculate the count without enumerating are pretty common, so I think it would make sense to expose a method that does it in a correct and complete way. That being said, I don't think it should be called public static class Enumerable
{
public static bool TryGetCount(IEnumerable<T> source, out int count);
} |
I agree it shouldn't be an extension method. I like the |
@TylerBrinkley would you be able to update the original post? I'll see if I can submit this for review. |
@eiriktsarpalis I've updated the original post. |
I think it will cause developers confused when returning Enumerable.TryGetCount(mySource, out int count)
I know it's reasonable but I prefer to handle it by users themself. Or, |
How so? The point of this addition is to provide its user a clear signal as to whether a count can be obtained without enumerating. Roughly it would translate into the following code: if (Enumerable.TryGetCount(source, out int count))
{
T[] fixedBuffer = new T[count];
ConsumeUsingFixedBuffer(source, fixedBuffer);
}
else
{
List<T> resizingBuffer = new List<T>();
ConsumeUsingResizingBuffer(source, resizingBuffer);
}
I think you are right in pointing out that the currently proposed name |
Well, it's better. |
I changed the proposal to |
namespace System.Linq
{
public static class Enumerable
{
public static bool TryGetNonEnumeratedCount(this IEnumerable<T> source, out int count);
}
} |
* implement Enumerable.TryGetEnumeratingCount * address feedback * update consistency tests * Replace EnumerableHelpers.TryGetCount with new method * Rename to method name as approved * make method is renamed in all projects
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.Count
extension method however if theIEnumerable<T>
isn't anICollection<T>
or any of the other interfaces or types with aCount
orLength
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
orLength
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 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
Count
with anallowEnumeration
boolean parameter.TryGetCount
.TryGetNonEnumeratedCount
.The text was updated successfully, but these errors were encountered: