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

If you are coding in C# then you can be pretty certain at some point you’ll need to use a C# List. This post will teach you how to initialize a C# list, how to add, and insert an item to a C# List, and also 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.

How to initialise a C# List

A C# List is a collection of strongly typed objects, and in order to use it you must import the correct namespace as follows:

using System.Collections.Generic

There’s a few ways to initialize a List and I’ll show examples of each. Which one you use will depend on the size of the List you are initialising, the data that will fill the List, and whether you have your data when you initialise it or not.

If you don’t have any of the data yet then your initialiser will be like one of these examples:

//List of object type Drink - see Drink model definition below
List<drink> = new List<Drink>;

//list of integers
List<int> = new List<int>;

//list of strings
List<string> = new List<string>;

This next example shows a simple C# collection intialiser, building a collection of integers:

List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Equally you could create a List of strings:

List<string> countries = new List<string> { France, USA, Germany, England};

Or you can initialize a list of objects using object initializers:

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

This example, and subsequent examples make use of typed Lists using the Drink object as defined here:

internal class Drink
{
   public string Name { get; set; }
   public int Id { get; set; }
}

Add an item to a 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 });

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

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

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