Linq Prepend Method in C#3

Introduction:

The LINQ Prepend method in C# is used to add an element at the beginning of an IEnumerable collection. It returns a new sequence that consists of the specified element followed by the elements of the source sequence. This method is particularly useful when you need to add an element to the beginning of an existing sequence without modifying the original sequence. In this lesson, we will explore how to use the LINQ Prepend method in C# and provide examples of its usage.

Syntax:

The syntax for the LINQ Prepend method is as follows:
public static IEnumerable<TSource> Prepend<TSource>(
    this IEnumerable<TSource> source,
    TSource element
)

The Prepend method takes an IEnumerable source as input and an element to be added at the beginning of the sequence. The method returns a new sequence that consists of the specified element followed by the elements of the source sequence.

Usage:

Here is an example of how to use the Prepend method to add an element to the beginning of an IEnumerable collection:
var numbers = new List<int> { 2, 3, 4, 5 };
var newNumber = 1;

var newNumbers = numbers.Prepend(newNumber);

foreach (var number in newNumbers)
{
    Console.WriteLine(number);
}

In this example, we have an IEnumerable collection called numbers. We want to add an element 1 to the beginning of this collection without modifying the original sequence. We use the Prepend method to create a new sequence that consists of the specified element followed by the elements of the source sequence. The result is a new sequence that contains the number 1 followed by the original numbers sequence.

It’s important to note that the Prepend method returns a new sequence and does not modify the original sequence. If you want to modify the original sequence, you can assign the result of the Prepend method to the original sequence variable.

Here is another example that shows how to use the Prepend method with a custom class:
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

var people = new List<Person>
{
    new Person { Name = "Alice", Age = 25 },
    new Person { Name = "Bob", Age = 30 },
    new Person { Name = "Charlie", Age = 35 }
};

var newPerson = new Person { Name = "Dave", Age = 40 };

var newPeople = people.Prepend(newPerson);

foreach (var person in newPeople)
{
    Console.WriteLine($"{person.Name} - {person.Age}");
}

In this example, we have a custom class called Person. We have a list of Person objects called people and we want to add a new person to the beginning of this list. We use the Prepend method to create a new sequence that consists of the specified Person object followed by the elements of the source sequence. The result is a new sequence that contains the new person object followed by the original people sequence.

Conclusion:

The LINQ Prepend method is a useful tool for adding an element at the beginning of an IEnumerable collection without modifying the original sequence. By using the Prepend method, you can easily create a new sequence that contains the specified element followed by the elements of the source sequence. The Prepend method is particularly useful when you need to add an element to the beginning of an existing sequence. When using the Prepend method, it’s important to keep in mind that it returns a new sequence and does not modify the original sequence.