Skip to content

[API Proposal]: Generic Enumerable.Sequence method #97689

Closed
@ladeak

Description

@ladeak

For latest proposed / accepted API, please see comment.

Background and motivation - UPDATED 2024/01/31

So that Enumerable.Range can be used with anything that implements IBinaryInteger<T>.

API Proposal

public static class Enumerable
{
    public static IEnumerable<T> Range<T>(T start, int count) where T : IBinaryInteger<T>
}

API Usage

public record struct MyInterval<T> where T : IBinaryInteger<T>
{
    public T Start { get; }
    public T End { get; }

    // ...
    
    public IEnumerable<T> GetAll() => Enumerable.Range(Start, End);

Alternative Designs

An alternative could be to handle all INumber<T>, in this case having a 3rd increment parameter could be reasonable

public static class Enumerable
{
    public static IEnumerable<T> Range<T>(T start, T end, T increment) where T : INumber<T>
}

Risks

The original proposal with 2 parameters overload for the Range method could be source-breaking, as the following case returns an IEnumerable<int> and not IEnumerablem<byte> today:

byte start = 0, count = 5;
IEnumerable<int> e = Enumerable.Range(start, count);

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Linqin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions