Linq Prepend Method in C#:

The Linq Prepend Method is used to add one value to the beginning of a sequence. This Prepend method like the Append method does not modify the elements of the sequence. Instead, it creates a copy of the sequence with the new element. The signature of this is given below.

Type Parameters
  1. TSource: The data type of the elements contained in the sequence.

Parameters:

  1. IEnumerable<TSource> source: A sequence of values.
  2. TSource element: The value to prepend at the beginning of the sequence.

Returns:

  1. IEnumerable<TSource>: A new sequence that begins with the element.

Exceptions: When the source is null, it will throw ArgumentNullException.

Note: This method is supported from Framework 4.7.1 or later.

Exmple:

The following example shows how to prepend a value to the beginning of the sequence using the Prepend method. The following example code is self-explained. So, please go through the comment lines.

 

In the next article, I am going to discuss the LINQ ZIP Method with an example. Here, in this article, I try to explain the Linq Prepend Method in C# with an example.

Leave a Comment