Home » C# » The C# list – how to add and insert items, with code examples

The C# list – how to add and insert items, with code examples

By Emily

If you are coding in C# then you can be pretty certain at some point you’ll need to use a C# List. I’ve already written a post which explains how to initialize a C# list, and this one explains how to add, or insert an item to a C# List. It also covers how to add a List to another List using AddRange() and InsertRange(). I’ll briefly cover each of these topics using code examples. In my examples I use a list of drinks (List<T> or in this case List<Drink>) to use a more real world example than just adding strings to a list of strings. List<T> represents a strongly typed list of objects that can be accessed by index, and it offers methods to search, sort, and filter. I’ll follow up this post with posts that show examples using those methods too.

Add an item to a C# List

This shows how to add an item to a List using the List.Add() method. The .Add method adds an item to the end of the List by default.

List<Drink> drinks = new List<Drinks>();
drinks.Add(new Drink{ Name = "Beer", Id=8 });
drinks.Add(new Drink{ Name = "Champagne", Id=312 });
drinks.Add(new Drink{ Name = "Lemonade", Id=14 });

Add one List to another List using AddRange

There’s another method you can use if you want to add items to a C# List – AddRange(). It allows you to add one List to another List.

//The List to start with

8: Beer
312: Champagne
14: Lemonade


List<Drink> extraDrinks = new List<Drink> {
   new Drink{ Name = "Sambucca", Id=9 },
   new Drink{ Name = "Prosecco", Id=192 },
   new Drink{ Name = "Vodka and orange", Id=17 } 
};

barOrder.AddRange(extraDrinks);

//The List now
8: Beer
312: Champagne
14: Lemonade
9: Sambucca
192: Prosecco
17: Vodka and orange

Add an item to the start of a C# List – Insert

If you want to add an item to the start of the List you would use the List.Insert() method which inserts an item to the given List at the position provided. So to insert an item to the start of the List you would specify the position as 0, which is the first position in the list.

//The List to start with

312: Champagne
74: Coke
88: Sparkling Water

//now insert an item into the first position
drinks.Insert(0, new Drink{ Name = "Orange Juice", Id=501 };

//The List now

501: Orange Juice
312: Champagne
74: Coke
88: Sparkling Water

              

But to insert an item to the 3rd row of the List you would specify the position as 2.

//The List to start with
501: Orange Juice
312: Champagne
74: Coke
88: Sparkling Water

drinks.Insert(2, new Drink{ Name = "Apple Juice", Id=502 });

//The List now
501: Orange Juice
312: Champagne
502: Apple Juice
74: Coke
88: Sparkling Water

Insert a List into another List using InsertRange

And similarly to the AddRange() method I talked about above, there is an InsertRange() method which allows you to insert a List into another List.

//The List to start with
501: Orange Juice
312: Champagne
502: Apple Juice
74: Coke
88: Sparkling Water

List<Drink> moreDrinks = new List<Drink> {
  new Drink{ Name = "Martini", Id=43 },
  new Drink{ Name = "Mojito", Id=44 },
  new Drink{ Name = "Pale Ale", Id=18 } };

//Insert this List to the 3rd row
drinks.InsertRange(2, moreDrinks);

//The List now
501: Orange Juice
312: Champagne
43: Martini
44: Mojito
18 Pale Ale
502: Apple Juice
74: Coke
88: Sparkling Water


Summary

You’ve now learned how to add or insert an item to a C~ List, and add or insert a List into another List. You ay find my other post useful as it describes how to remove an item from a C# list.

You may want to use a C# Dictionary instead in which case read my other post about using the C# Dictionary.