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

[API Proposal]: Add IsEmpty property to collections and arrays. #79769

Open
aKzenT opened this issue Dec 16, 2022 · 2 comments
Open

[API Proposal]: Add IsEmpty property to collections and arrays. #79769

aKzenT opened this issue Dec 16, 2022 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Collections blocked Issue/PR is blocked on something - see comments
Milestone

Comments

@aKzenT
Copy link

aKzenT commented Dec 16, 2022

Background and motivation

See discussion in #75933 .

For performance reason using LINQ ".Any()" to check if a collection is empty is discouraged in favour of checking if Length and Count is greater than zero. However arguably using "Any()" expresses the intent more clearer and there could even be collection implementations where computing Count is more expensive than just checking if the collection is empty or not.

Adding an IsEmpty property would not only solve that issue but also align with other APIs that have an IsEmpty property like Span.

API Proposal

Since there are probably millions of existing implementations of collection types, using a default implementation that checks the Count might be the best option.

namespace System.Collections.Generic;

public interface ICollection<T>
{
    bool IsEmpty => Count == 0; 
}

API Usage

// Fancy the value
var c = new List<int>();

if(c.IsEmpty)
{

}

Alternative Designs

Improve performance of using Any() might solve part of the problem.

Risks

No response

@aKzenT aKzenT added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Dec 16, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Dec 16, 2022
@ghost
Copy link

ghost commented Dec 16, 2022

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

Issue Details

Background and motivation

See discussion in #75933 .

For performance reason using LINQ ".Any()" to check if a collection is empty is discouraged in favour of checking if Length and Count is greater than zero. However arguably using "Any()" expresses the intent more clearer and there could even be collection implementations where computing Count is more expensive than just checking if the collection is empty or not.

Adding an IsEmpty property would not only solve that issue but also align with other APIs that have an IsEmpty property like Span.

API Proposal

Since there are probably millions of existing implementations of collection types, using a default implementation that checks the Count might be the best option.

namespace System.Collections.Generic;

public interface ICollection<T>
{
    bool IsEmpty => Count == 0; 
}

API Usage

// Fancy the value
var c = new List<int>();

if(c.IsEmpty)
{

}

Alternative Designs

Improve performance of using Any() might solve part of the problem.

Risks

No response

Author: aKzenT
Assignees: -
Labels:

api-suggestion, area-System.Collections

Milestone: -

@eiriktsarpalis
Copy link
Member

We generally avoid adding DIMs to existing interfaces because it can result in both source and binary breaking changes, cf. #55106.

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Dec 19, 2022
@eiriktsarpalis eiriktsarpalis added this to the Future milestone Dec 19, 2022
@eiriktsarpalis eiriktsarpalis added the blocked Issue/PR is blocked on something - see comments label Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Collections blocked Issue/PR is blocked on something - see comments
Projects
None yet
Development

No branches or pull requests

2 participants