๐ŸŽ“ A1Learner
๐Ÿ”

C# Lists

A List is a collection that holds values in order and can grow or shrink. The type in angle brackets names the type of every item.

Add and Count Items

This program needs a second using line for List. Two names go in, and the first one prints.

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<string> names = new List<string>();
        names.Add("Hana");
        names.Add("Bo");
        Console.WriteLine(names[0]);
        Console.WriteLine(names.Count);
    }
}

Remove and Check Items

Remove takes one fruit out. Contains then answers for a removed item and a kept item.

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<string> fruits = new List<string>();
        fruits.Add("Apple");
        fruits.Add("Banana");
        fruits.Add("Kiwi");
        fruits.Remove("Banana");
        Console.WriteLine(fruits.Contains("Banana"));
        Console.WriteLine(fruits.Contains("Kiwi"));
    }
}

Sort and Print Items

Sort puts the names in alphabetical order. A foreach loop then prints each name.

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<string> fruits = new List<string>();
        fruits.Add("Pear");
        fruits.Add("Apple");
        fruits.Add("Kiwi");
        fruits.Sort();
        foreach (string fruit in fruits)
        {
            Console.WriteLine(fruit);
        }
    }
}

Example

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<string> shopping = new List<string>();
        shopping.Add("Milk");
        shopping.Add("Bread");
        shopping.Add("Eggs");
        shopping.Add("Cheese");
        shopping.Remove("Bread");
        shopping.Sort();
        foreach (string item in shopping)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine("Items left: " + shopping.Count);
    }
}

Key points

  • List<T>: a collection type from System.Collections.Generic.
  • Use Add to put an item at the end of a list.
  • Read one item with its index in square brackets.
  • Count: a property that holds how many items the list holds.
  • Use Remove to take the first matching item out of a list.
  • Contains reports whether an item is in the list.
  • Use Sort to order the items in a list.

In this example, the list starts empty and four items go in with Add. Remove takes Bread out, so three items stay. Sort orders the three items alphabetically. The foreach loop prints Cheese, Eggs, and Milk in that order. The last line prints the Count, which is 3.

Note

Count is a property, so it takes no parentheses. Write list.Count, not list.Count().

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Programiz C# Compiler

Exercise

A list holds three items and one more is added with Add. What does the Count property then hold?