Skip to content

Please add Empty to ReadOnlyCollection<T> #41147

@thargol1

Description

@thargol1

Background and Motivation

As it is a best practice for API's to return ReadOnlyCollection<T> instead of List<T> etc, it would be nice to have an easy way to create an empty ReadOnlyCollection<T>. On par with Array.Empty<T>().

As it is also a best practice to return an empty collection instead of null.

Proposed API

public class ReadOnlyCollection<T>
{
    public static ReadOnlyCollection<T> Empty { get; }
}

Usage Examples

public ReadOnlyCollection<Stuff> FindStuff(string filter)
{
    if (string.IsNullOrEmpty(filter)) return ReadOnlyCollection<Stuff>.Empty;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.CollectionswishlistIssue we would like to prioritize, but we can't commit we will get to it yet

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions