list.add c#

  1. HowTo
  2. C# Howtos
  3. Add List to Another List in C#

Add List to Another List in C#

Csharp Csharp List

Created: March-21, 2021

This tutorial will discuss methods to add one lists elements at the end of another list in C#.

Add a List to Another List With the List.AddRange[] Function in C

The easiest method for appending one lists elements at the end of the other list is to use the List.AddRange[] method in C#. The List.AddRange[x] method adds the elements of the collection x in the list. The following code example shows us how to add one list to another list with the List.AddRange[] function in C#.

using System; using System.Collections.Generic; namespace add_list { static void Main[string[] args] { List first = new List { "do", "rey", "me" }; List second = new List { "fa", "so", "la", "te" }; first.AddRange[second]; foreach[var e in first] { Console.WriteLine[e]; } } } }

Output:

do rey me fa so la te

We created and initialized 2 lists of strings, first and second, in the above code. We appended the second list elements at the end of the first list with the first.AddRange[second] function. In the end, we displayed the elements of the first list.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp List

  • Get the Last Element of a List in C#
  • Copy a List in C#
    • async and await in C#
    • Add String to an Array in C#
    report this ad

    Video liên quan

    Chủ Đề