Linq Append Method in C#

Linq Append Method in C# with Example

In this article, I am going to discuss the LINQ Append Method in C# with an example. Please read our previous article before proceeding to this article where we discussed the LINQ Empty Method with some examples.

Linq Append Method in C#:

The LINQ Append Method is used to append a value to the end of the sequence. This Append method does not modify the elements of the sequence. Instead, it creates a copy of the sequence with the new element. Following is the signature of the Append method.

Linq Append Method in C# with Example

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 append at the end of the sequence.

Returns:

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

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

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

Example:

The following example shows how to use the Append Method to append a value to the end of the sequence. The following example is self-explained. So, please go through the comment lines.

using System.Linq;
using System.Collections.Generic;
using System;
namespace LinqDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creating a list of integer
            List<int> intSequence = new List<int> { 10, 20, 30, 40 };

            // Trying to append 50 at the end of the intSequence
            intSequence.Append(5);

            //It doesn't work because the original list has not been changed
            Console.WriteLine(string.Join(", ", intSequence));

            // It works now because we are using a changed copy of the original sequence
            Console.WriteLine(string.Join(", ", intSequence.Append(5)));

            // Creating a new sequence explicitly
            List<int> newintSequence = intSequence.Append(5).ToList();

            // Printing the new sequence in the console
            Console.WriteLine(string.Join(", ", newintSequence));

            Console.ReadKey();
        }
    }
}

Output:

Linq Append Method in C# with Example

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

Leave a Reply

Your email address will not be published. Required fields are marked *