Skip to content
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

TryGetNonEnumeratedCount should return a count for IReadOnlyCollection<T> #61396

Closed
burkenyo opened this issue Nov 10, 2021 · 3 comments
Closed
Labels
area-System.Linq untriaged New issue has not been triaged by the area owner

Comments

@burkenyo
Copy link

burkenyo commented Nov 10, 2021

Description

Users of Enumerable.TryGetNonEnumeratedCount() may expect the API to return true for a collection that only implements IReadOnlyCollection<T>. Instead, it returns false.

Reproduction Steps

create a new .NET 6 console app and run this code:

using System.Collections;

Console.WriteLine(Enumerable.TryGetNonEnumeratedCount(new A(), out _));

class A : IReadOnlyCollection<int>
{
    public int Count => 0;

    public IEnumerator<int> GetEnumerator()
    {
        yield break;
    }

    IEnumerator IEnumerable.GetEnumerator() =>
        GetEnumerator();
}

Expected behavior

True is written to the console, indicating the collection was able to return a count sans enumeration.

Actual behavior

False is written to the console, indicating the collection was not able to return a count sans enumeration.

Regression?

No, this is a new API.

Known Workarounds

Test the collection size manually.

Configuration

No response

Other information

If this was a conscious decision, feel free to close this issue.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Linq untriaged New issue has not been triaged by the area owner labels Nov 10, 2021
@ghost
Copy link

ghost commented Nov 10, 2021

Tagging subscribers to this area: @dotnet/area-system-linq
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Users of Enumerable.TryGetNonEnumeratedCount() may expect the API to return true for a collection that only implements IReadOnlyCollection. Instead, it returns false.

Reproduction Steps

create a new .NET 6 console app and run this code:

using System.Collections;

Console.WriteLine(Enumerable.TryGetNonEnumeratedCount(new A(), out _));

class A : IReadOnlyCollection<int>
{
    public int Count => 0;

    public IEnumerator<int> GetEnumerator()
    {
        yield break;
    }

    IEnumerator IEnumerable.GetEnumerator() =>
        GetEnumerator();
}

Expected behavior

True is written to the console, indicating the collection was able to return a count sans enumeration.

Actual behavior

False is written to the console, indicating the collection was not able to return a count sans enumeration.

Regression?

No, this is a new API.

Known Workarounds

Test the collection size manually.

Configuration

No response

Other information

If this was a conscious decision, feel free to close this issue.

Author: burkenyo
Assignees: -
Labels:

area-System.Linq, untriaged

Milestone: -

@stephentoub
Copy link
Member

This was by-design:
#48239 (comment)

Various issues have tracked revisiting IReadOnlyCollection-special-casing in LINQ all-up. The current one I believe is:
#42254

@burkenyo
Copy link
Author

Thanks for the quick reply, closing.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Linq untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants