C# Add Multiple Items to List

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:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace Example
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. List<string> list = new List<string>()
  10. {
  11. "Apple", "Orange", "Kiwi", "Melon"
  12. };
  13. list.Add("Mango");
  14. list.Add("Watermelon");
  15.  
  16. foreach (string item in list)
  17. {
  18. Console.WriteLine("Item: {0}", item);
  19. }
  20. }
  21. }
  22. }

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:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace Example
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. string[] str = new string[3] { "Item 3", "Item 4", "Item 5" };
  10.  
  11. List<string> list = new List<string>()
  12. {
  13. "Item 1", "Item 2"
  14. };
  15.  
  16. list.AddRange(str);
  17. foreach (string item in list)
  18. {
  19. Console.WriteLine("Item: {0}", item);
  20. }
  21. }
  22. }
  23. }

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":

  1. using System;
  2. using System.Collections.Generic;
  3. namespace Example
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. List<int> list1 = new List<int>()
  10. {
  11. 3, 4, 5
  12. };
  13.  
  14. List<int> list2 = new List<int>()
  15. {
  16. 1, 2
  17. };
  18.  
  19. list2.AddRange(list1);
  20. foreach (int item in list2)
  21. {
  22. Console.WriteLine("Item: {0}", item);
  23. }
  24. }
  25. }
  26. }

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:

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Example
  5. {
  6. public class Program
  7. {
  8. public static void Main(string[] args)
  9. {
  10. List<string> list1 = new List<string>()
  11. {
  12. "Item 3", "Item 4", "Item 5"
  13. };
  14.  
  15. List<string> list2 = new List<string>()
  16. {
  17. "Item 1", "Item 2"
  18. };
  19. foreach (string item in list1)
  20. {
  21. list2.Add(item);
  22. }
  23. foreach (string item in list2)
  24. {
  25. Console.WriteLine("{0}", item);
  26. }
  27. }
  28. }
  29. }

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.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables