C# List Add & Insert
last modified July 5, 2023
In this article we show how to add new elements to a list in C#.
C# list is a collection of elements of the same type. The elements can be accessed by index.
In C#, we can use the Add
, AddRange
,
Insert
, and InsertRange
methods to add elements to a
list.
C# List Add
The Add
method appends an element to a list.
var words = new List<string> { "sky", "war", "crypto" }; words.Add("water"); Console.WriteLine(string.Join(",", words)); words.Add("falcon"); Console.WriteLine(string.Join(",", words)); words.Add("soap"); Console.WriteLine(string.Join(",", words));
We define a list of words. Using Add
, we append three new
words.
$ dotnet run sky,war,crypto,water sky,war,crypto,water,falcon sky,war,crypto,water,falcon,soap
C# List AddRange
The AddRange
method appends elements of a collection to the list.
var words = new List<string> { "sky", "war", "crypto" }; var words2 = new List<string> { "falcon", "soap" }; var words3 = new string[] { "book", "cloud" }; var words4 = new HashSet<string> { "money", "dog" }; Console.WriteLine(string.Join(",", words)); words.AddRange(words2); Console.WriteLine(string.Join(",", words)); words.AddRange(words3); Console.WriteLine(string.Join(",", words)); words.AddRange(words4); Console.WriteLine(string.Join(",", words));
In the example, we add elements of another list, array, and set to the initial list of strings.
$ dotnet run sky,war,crypto sky,war,crypto,falcon,soap sky,war,crypto,falcon,soap,book,cloud sky,war,crypto,falcon,soap,book,cloud,money,dog
C# List Insert
The Insert
method inserts an element into a list at the specified
index.
var words = new List<string> { "sky", "war", "crypto" }; words.Insert(0, "storm"); words.Insert(words.Count, "fortress"); Console.WriteLine(string.Join(",", words));
In the program, we insert a word at the beginning and at the end with
Insert
.
$ dotnet run storm,sky,war,crypto,fortress
C# List InsertRange
The InsertRange
method inserts elements of a collection at the
specified index.
var words = new List<string> { "sky", "war", "crypto" }; var words2 = new List<string> { "falcon", "soap" }; var words3 = new string[] { "book", "cloud" }; var words4 = new HashSet<string> { "money", "dog" }; Console.WriteLine(string.Join(",", words)); words.InsertRange(0, words2); Console.WriteLine(string.Join(",", words)); words.InsertRange(0, words3); Console.WriteLine(string.Join(",", words)); words.InsertRange(words.Count, words4); Console.WriteLine(string.Join(",", words));
In the program, we insert elements of another list, array, and set to an initial list of strings.
$ dotnet run sky,war,crypto falcon,soap,sky,war,crypto book,cloud,falcon,soap,sky,war,crypto book,cloud,falcon,soap,sky,war,crypto,money,dog
Source
List class - language reference
In this article we have showed how add new elements to a list in C#.
Author
List all C# tutorials.