LINQ Interview Question #133

What is Append and Prepend in LINQ?

Advanced LINQ, Practical Problems & Senior Interview Scenarios Senior Advanced

Quick Interview Answer

Append returns a sequence with one additional element at the end, while Prepend places an element at the beginning. They do not modify the original source collection.

Detailed Explanation

Append returns a sequence with one additional element at the end, while Prepend places an element at the beginning. They do not modify the original source collection.

These operators are convenient for composing sequences without manually creating a new list simply to add one boundary element.

Code Example

var numbers = new[] { 2, 3 };

var result = numbers
    .Prepend(1)
    .Append(4);

// 1, 2, 3, 4