[API Proposal]: Create Generalized Pattern for Collection Builder Types

Open
#112,990 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
backend

Research direction

Start by reading this proposal alongside #111715 and the existing CollectionBuilderAttribute mentioned in the issue. Compare the proposed generalized attribute with the current IEnumerable/IList special cases and determine the API design and compatibility requirements. Done means the generalized mechanism is sufficiently designed for a follow-up implementation proposal.

Written by the indexing model from the issue text.

Description

api-suggestion area-System.Collections
Background and motivation

Currently, as of .NET 9, there is no generic mechanism for defining collection builder types. This results in collection builders being special cased, currently IEnumerable<T> => T[] and IList<T> => List<T>. There may be others but these are the two most common scenarios.

This mechanism allows for situations like this:

public IEnumerable<T> EnumerateFoos<T>(IEnumerable<T> foos)
{
    if (foos is null)
    {
        return []; // returns an empty T[]
    }

    // Not empty, evaluate
}

#111715 proposes adding this capability for IAsyncEnumerable<T> but there is no mechanim that can be extended in a generic manner. Instead, they must be special typed just like arrays and lists. This proposal is to add a generic mechanism that can be used to decorate any current or future type.

That way we're not just continually fixing things by adding "just one more type" for each release.

API Proposal
[AttributeUsage(AttributeTargets.Class, AllowMultiple: true)]
internal sealed class CollectionBuilderForAttribute(Type enumerableType) : Attribute
{
}

I'm not entirely settled on the API design at this point but the idea is that this attribute becomes the primary driver for defining collection builders. This allows for a general, well-known, and documented solution to adding support for collection types moving forward. This should not resolve #111715 directly, but should provide a mechanism for a separate API proposal which implements the solution provided here.

API Usage

This should be a rough approximation of the special casing that List has for IList.

[CollectionBuilderFor(typeof(IList<>))]
public sealed class List<T> : IList<T>
{
    // Body omitted
}
Alternative Designs

We could stay our current course and set up a handler in the runtime/corelib, similar to how IEnumerable is handled by T[] and IList is handled by List. This is a lot of special casing for a feature that is repeated multiple times.

In .NET 10, we gain CollectionBuilderAttribute but this would not allow support to be backported to previous TFMs.

Risks

None that I'm aware of.

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.