In C#, List.Add(item) puts one item at the end of a list. If the list had 3 items, the new item gets index 3, and Count becomes 4. Think of writing one more line at the bottom of a shopping list.
Unlike an array, a list can grow, so you never have to say how many items it will hold. Add returns nothing (void), and it never rejects a value. The same value can go in many times. To put an item somewhere other than the end, use Insert().
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.Add(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Call Add on a list variable and pass the item. The item must match the type of the list.
C#
List<string> fruits = new List<string>();
fruits.Add("apple"); // index 0
fruits.Add("banana"); // index 1
fruits.Add("apple"); // index 2, a repeat is fine
Call
What it does
fruits.Add("apple")
Appends "apple" at the end. Count grows by 1.
numbers.Add(10)
Same idea for a List<int>. The value is stored, not a reference to the variable.
names.Add(null)
Allowed for reference types such as string. The list then holds a null item.
Add is O(1) most of the time. When the hidden array is full, the list allocates a bigger one and copies the items, which is slower but happens rarely. See EnsureCapacity() to avoid repeated growing. To add many items in one call, use AddRange().
Library declarations
C#
public void Add(T item);
Parameters
Parameter
Description
item
Required. The item to add. It must be of type T. A null is allowed when T is a reference type.
Return Value
Return Value
Description
void
Nothing is returned. The list changes in place, and Count increases by 1.
C# Examples
Example 1 — Add Three Fruits
Add three fruits to an empty list, then print the size and read two items by index.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> fruits = new List<string>();
fruits.Add("apple");
fruits.Add("banana");
fruits.Add("cherry");
Console.WriteLine(fruits.Count);
Console.WriteLine(fruits[0]);
Console.WriteLine(fruits[2]);
Console.WriteLine(string.Join(", ", fruits));
}
}
Output
3
apple
cherry
apple, banana, cherry
How It Works
Empty start.new List<string>() has Count 0, so the first Add gives "apple" the index 0.
Always the end. Each later call puts its item after the last one, so "banana" is at index 1 and "cherry" at index 2.
Count tracks it. Three calls to Add means Count is 3. The last valid index is always Count - 1.
Order is kept.string.Join prints the items in the order they were added, so this output never changes.
Example 2 — Add the Same Value Twice
Add a number more than once and see that the list keeps every copy.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> scores = new List<int>();
scores.Add(80);
scores.Add(95);
scores.Add(80);
scores.Add(80);
Console.WriteLine(scores.Count);
Console.WriteLine(string.Join(" ", scores));
Console.WriteLine(scores.IndexOf(80));
Console.WriteLine(scores.LastIndexOf(80));
}
}
Output
4
80 95 80 80
0
3
How It Works
No rejection. A list never refuses a value. Three of the four calls added 80, and all three are stored.
Count counts copies. The result is 4, not 2, because the list counts every stored item, not just the different values.
First and last match.IndexOf(80) finds the first copy at index 0. LastIndexOf(80) finds the final copy at index 3.
Different from a set. If you want each value only once, a HashSet does that job. A list is for ordered items that may repeat.
Example 3 — Two Variables, One List
A list is a class, so a second variable can point to the same list. Add through one variable and read through the other. Also add a null.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> first = new List<string>();
List<string> second = first;
second.Add("shared");
first.Add(null);
Console.WriteLine(first.Count);
Console.WriteLine(second[0]);
Console.WriteLine(first[1] == null);
Console.WriteLine(ReferenceEquals(first, second));
}
}
Output
2
shared
True
True
How It Works
Copy of the reference.second = first does not copy the items. Both variables now point to one list object in memory.
Either name works. The first Add goes through second, and the second goes through first. Both end up in the same list, so Count is 2.
Null is a real item.first[1] holds null, so the comparison prints True. It still counts toward Count.
Need a separate copy? Write new List<string>(first) to build a second list with the same items. Then the two lists change independently.
Frequently Asked Questions
It adds one item at the end of the list and increases Count by 1. The list grows by itself when it needs more room.
No. Add returns void. If you need to know whether a value was already present, call Contains first, or use a HashSet, whose Add returns a bool.
Yes. A List<T> keeps every item you add, including repeated values and null (for reference types).
Add always puts the item at the end. Insert(index, item) puts it at a position you choose and moves the later items one place to the right.
Arrays have a fixed length, so they have no Add method. Use a List<T>, or create a bigger array with Array.Resize.