How to Add Multiple Items to a List in C#
In this tutorial, you will learn how to add multiple items to a list in C#.
To add an item to a list, we use the List<T>.Add() method. To add multiple items, we use List.AddRange() method.
Learn more:
For example, suppose you have a list of strings like the following:
List<string> list = new List<string>() {"Apple", "Orange", "Kiwi", "Melon"};
If you want to add the text strings "Mango" and "Watermelon" to the above list, you would write the following:
list.Add("Mango");
list.Add("Watermelon");
The following is a sample program:
using System; using System.Collections.Generic; namespace Example { public class Program { public static void Main(string[] args) { List<string> list = new List<string>() { "Apple", "Orange", "Kiwi", "Melon" }; list.Add("Mango"); list.Add("Watermelon"); foreach (string item in list) { Console.WriteLine("Item: {0}", item); } } } }
Output:
Item: Apple
Item: Orange
Item: Kiwi
Item: Melon
Item: Mango
Item: Watermelon
The List<T>.Add() method can only add one item at a time, so if you want to add multiple items at once, you need to use the List<T>.AddRange() method.
The syntax of the List<T>.AddRange() method is as follows:
List<T>.AddRange(IEnumerable<T> collection)
Let's take a look at the examples below:
The following sample program adds an array "str" of text strings with three elements to a list "list" with two elements:
using System; using System.Collections.Generic; namespace Example { public class Program { public static void Main(string[] args) { string[] str = new string[3] { "Item 3", "Item 4", "Item 5" }; List<string> list = new List<string>() { "Item 1", "Item 2" }; list.AddRange(str); foreach (string item in list) { Console.WriteLine("Item: {0}", item); } } } }
Output:
Item: Item 1 Item: Item 2 Item: Item 3 Item: Item 4 Item: Item 5
The following program adds the list of numbers "list1" to another list "list2":
using System; using System.Collections.Generic; namespace Example { class Program { static void Main(string[] args) { List<int> list1 = new List<int>() { 3, 4, 5 }; List<int> list2 = new List<int>() { 1, 2 }; list2.AddRange(list1); foreach (int item in list2) { Console.WriteLine("Item: {0}", item); } } } }
Output:
Item: 1
Item: 2
Item: 3
Item: 4
Item: 5
You can also loop through the list and add its items to a specified list.
The following sample code shows how to loop through a list and add its items to another list:
using System; using System.Collections.Generic; namespace Example { public class Program { public static void Main(string[] args) { List<string> list1 = new List<string>() { "Item 3", "Item 4", "Item 5" }; List<string> list2 = new List<string>() { "Item 1", "Item 2" }; foreach (string item in list1) { list2.Add(item); } foreach (string item in list2) { Console.WriteLine("{0}", item); } } } }
Output:
Item: Item 1 Item: Item 2 Item: Item 3 Item: Item 4 Item: Item 5
In this C# tutorial, we have discussed how to add multiple items to a list in C# programming.