Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Append/Prepend #14728

@eatdrinksleepcode

Description

@eatdrinksleepcode

Summary

Two of the IEnumerable extension methods that I carry in my collection (we all have a personal collection of IEnumerable extension methods, am I right?) are Append and Prepend, for adding a single element to the end or beginning of a sequence respectively. Though not quite as common as concatenating sequences, I have been surprised at how often this need arises. Doing this inside of an expression today is not straightforward; the best equivalent that I have found is far from ideal:

sequence.Concat(Enumerable.Repeat(singleValue, 1))

I propose adding Prepend and Append extension methods to Enumerable to fill this need.

API

public static class Enumerable
{
    public static IEnumerable<T> Append<T>(this IEnumerable<T> source, T value);
    public static IEnumerable<T> Prepend<T>(this IEnumerable<T> source, T value)
}

Usage

optionalValues.Prepend("<None>")

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions