Skip to content

Enumerable.Prepend and Enumerable.Append's remark "it creates a copy of the collection with the new element." can be misleading. #11610

@andern

Description

@andern

Type of issue

Missing information

Description

The remarks for both Enumerable.Append and Enumerable.Prepend can be misleading.

It currently states for both:

This method does not modify the elements of the collection. Instead, it creates a copy of the collection with the new element.

However it returns a lazily evaluated wrapper:

var original = new List<string> { "b", "c" };
var result = original.Prepend("a");

Console.WriteLine(result.GetType()); // System.Linq.Enumerable+PrependIterator`1

and no copy is actually made:

var list = new List<string> { "b", "c" };
var result = list.Prepend("a");

list.Add("d");

foreach (var item in result)
    Console.WriteLine(item);

// Output:
// a
// b
// c
// d

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.prepend?view=net-8.0#remarks

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Linq/Enumerable.xml

Document Version Independent Id

9556f661-a131-5b65-913f-50770cc980cf

Platform Id

acda5d20-c39a-4d6d-e70e-28d92c16a6b3

Article author

@dotnet-bot

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions