C# List – Add Element with List.Add()
Use List<T>.Add() to add one element to the end of a C# List<T>. The item must be compatible with the list’s element type. For example, a List<int> accepts integers, while a List<string> accepts strings.
The Add() method changes the existing list. It does not return the added item or a new list.
C# List.Add() syntax and how it appends an element
To add an element to the C# List, use List.Add() method. The definition of of List.Add() is given below.
void List<T>.Add(T item)
Add() method returns nothing. item/element passed as the argument to Add() method should match the type of List items.
T represents the element type of the list, and item is the value to add. Add() places the new item after the current last element, so the list’s Count increases by one.
Example 1 – Add integers to a C# List with Add()
In this example, we shall shall create a List of integers, and then add items to it using Add() method.
Program.cs
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
//empty list
List<int> nums = new List<int>();
//add elements to list
nums.Add(63);
nums.Add(58);
nums.Add(47);
//print list
foreach (int num in nums) {
Console.WriteLine(num);
}
}
}
Run the above C# program. The three items shall be added to the list.
Output
63
58
47
Each call to Add() appends one integer. The order in which the values are added is therefore the same order shown when the list is enumerated.
Add a string to a C# List<string>
The same method works with other generic list types. For a list of strings, pass a string to Add().
List<string> names = new List<string>();
names.Add("Ava");
names.Add("Noah");
Console.WriteLine(names[0]);
Console.WriteLine(names[1]);
Output
Ava
Noah
Example 2 – Add an object to a C# List
In this example, we shall add custom class objects to the given list.
We shall define a class named Car. In the Main method, we shall create a List that can contain elements of type Car. Then we shall create objects of type Car and add to the list.
Program.cs
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
//create list
List<Car> cars = new List<Car>();
//add objects to the list
cars.Add(new Car("Toyota", 1250000));
cars.Add(new Car("Tata", 1300000));
cars.Add(new Car("Honda", 1150000));
//print
foreach (Car car in cars) {
Console.WriteLine(car.name + " - "+car.price);
}
}
}
class Car{
public string name;
public int price;
public Car(string name, int price){
this.name = name;
this.price = price;
}
}
Run the above C# program.
Output
Toyota - 1250000
Tata - 1300000
Honda - 1150000
Because the list is declared as List<Car>, each argument passed to Add() is a Car object.
Example 3 – Adding the wrong data type to a C# List
When you try to add an element of a different datatype, you will get an error saying argument cannot be converted.
In this example, we have initialized a list of integers. But, then we try to add an element of type string.
Program.cs
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
//empty list
List<int> nums = new List<int>();
//add elements to list
nums.Add(63);
nums.Add(58);
nums.Add("hello");
//print list
foreach (int num in nums) {
Console.WriteLine(num);
}
}
}
Run the above C# program.
Output
D:\workspace\csharp\HelloWorld\Program.cs(10,18): error CS1503: Argument 1: cannot convert from 'string' to 'int' [D:\workspace\csharp\HelloWorld\HelloWorld.csproj]
The compiler rejects "hello" because the list was declared as List<int>. Use a value of the correct type, or use a different list type when the program genuinely needs to store different data.
Add multiple elements to a C# List with AddRange()
Add() accepts one item per call. When you already have several values in an array, list, or another compatible collection, AddRange() is the direct way to append all of them to the existing list.
list.AddRange(collection);
For example:
List<int> numbers = new List<int> { 10, 20 };
int[] moreNumbers = { 30, 40, 50 };
numbers.AddRange(moreNumbers);
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Output
10
20
30
40
50
Append another C# List with AddRange()
You can also append one list to another when both contain compatible element types. The source list is not replaced; its elements are added to the end of the destination list.
List<string> first = new List<string> { "red", "green" };
List<string> second = new List<string> { "blue", "black" };
first.AddRange(second);
Console.WriteLine(string.Join(", ", first));
Output
red, green, blue, black
Add an element at the beginning or a specific index with List.Insert()
Add() always places the item at the end. If the item must go at a particular zero-based index, use Insert(index, item). To add an item to the front of a list, insert it at index 0.
List<string> colors = new List<string> { "green", "blue" };
colors.Insert(0, "red");
colors.Insert(2, "yellow");
Console.WriteLine(string.Join(", ", colors));
Output
red, green, yellow, blue
The index passed to Insert() can range from 0 through the current Count. Using Count as the index inserts the item at the end, although Add() is clearer when appending is the only goal.
C# List.Add(), AddRange(), and Insert() compared
| Method | Use it when | Position |
|---|---|---|
Add(item) | You want to add one element | End of the list |
AddRange(collection) | You want to add multiple elements from a compatible collection | End of the list |
Insert(index, item) | You want to add one element at a specific index | The specified index |
Key points for adding elements to a C# List
- Use
Add()to append one item to the end of aList<T>. - The added value must be compatible with the list’s generic type
T. - Use
AddRange()when adding multiple items from another collection. - Use
Insert()when the new item belongs at the beginning or another specific index. - Adding an item increases the list’s
Count; existing items remain in the list.
In this C# Tutorial, we learned how to add an item to C# List, using List.Add() method.
TutorialKart.com