List<T>.Insert() / Count
| Since: | C# 2.0(2005) |
|---|
The Insert() method inserts an element at a specified position in a list, and the Count property returns the current number of elements in the list.
Syntax
using System.Collections.Generic; // Inserts an element at the specified index. list.Insert(int index, T item) // Inserts all elements of a collection at the specified index. list.InsertRange(int index, IEnumerable<T> collection) list.Count
Member List
| Member | Description |
|---|---|
| Insert(int index, T item) | Inserts an element at the specified index. All subsequent elements are shifted back by one. |
| InsertRange(int index, IEnumerable<T> collection) | Inserts all elements of a collection at the specified index. |
| Count | Returns the current number of elements in the list as an integer. Updated automatically whenever elements are added or removed. |
Sample Code
Use Insert() to insert an element at a specified position. Use index 0 to insert at the beginning.
Program.cs
using System;
using System.Collections.Generic;
List<string> ranking = new List<string> { "Gojo Satoru", "Ryomen Sukuna", "Itadori Yuji" };
Console.WriteLine($"Count: {ranking.Count}"); // Count: 3
ranking.Insert(1, "Fushiguro Megumi");
Console.WriteLine(string.Join(", ", ranking));
Console.WriteLine($"Count: {ranking.Count}"); // Count: 4
ranking.Insert(0, "Kugisaki Nobara");
Console.WriteLine(string.Join(", ", ranking));
This produces the following output:
dotnet run Count: 3 Gojo Satoru, Fushiguro Megumi, Ryomen Sukuna, Itadori Yuji Count: 4 Kugisaki Nobara, Gojo Satoru, Fushiguro Megumi, Ryomen Sukuna, Itadori Yuji
Inserting Multiple Elements with InsertRange()
InsertRange() lets you insert an entire collection at a specified position in one call. This is more efficient than calling Insert() one element at a time.
InsertRangeSample.cs
using System;
using System.Collections.Generic;
List<int> numbers = new List<int> { 1, 5 };
numbers.InsertRange(1, new int[] { 2, 3, 4 });
Console.WriteLine(string.Join(", ", numbers)); // 1, 2, 3, 4, 5
List<string> words = new List<string> { "start", "end" };
words.InsertRange(1, new string[] { "mid1", "mid2", "mid3" });
Console.WriteLine(string.Join(", ", words));
Console.WriteLine($"Count: {words.Count}");
This produces the following output:
dotnet run 1, 2, 3, 4, 5 start, mid1, mid2, mid3, end Count: 5
Using Count in Loop Processing
Count is a read-only property that returns the number of elements. It is automatically updated whenever elements are added or removed. It is commonly used in for loops and capacity checks.
CountSample.cs
using System;
using System.Collections.Generic;
List<int> evens = new List<int>();
for (int i = 2; i <= 10; i += 2)
{
evens.Add(i);
}
Console.WriteLine($"Even count: {evens.Count}"); // Even count: 5
for (int i = 0; i < evens.Count; i++)
{
Console.Write(evens[i] + " ");
}
Console.WriteLine();
This produces the following output:
dotnet run Even count: 5 2 4 6 8 10
Common Mistakes
Common Mistake: List<T> Does Not Have a Length Property
Arrays have a Length property, but List<T> does not. Using Length on a list — confusing it with an array — causes a compile error.
using System.Collections.Generic;
// NG: List<T> has no Length property → compile error
List<string> items = new List<string> { "a", "b", "c" };
// Console.WriteLine(items.Length); // CS1061: 'List<string>' does not contain a definition for 'Length'
using System;
using System.Collections.Generic;
// OK: use Count instead
List<string> items = new List<string> { "a", "b", "c" };
Console.WriteLine(items.Count); // 3
This produces the following output:
dotnet run 3
Also, passing a value greater than Count to Insert()'s index argument throws an ArgumentOutOfRangeException. To append to the end, use Add(item) rather than Insert(Count, item).
Notes
Count is the list equivalent of the array Length property. Because List<T> does not have a Length property, calling Length by mistake — confusing it with an array — will cause a compile error. Always use Count to get the number of elements in a list.
Insert() must shift all elements after the insertion point, so the larger the list, the higher the processing cost. If you frequently need to insert at the beginning or in the middle of a collection, consider using LinkedList<T> instead.
For searching and checking elements, see 'List<T>.Contains() / IndexOf()'.
If you find any errors or copyright issues, please contact us.