Lesson Weekend

If we want to create a collection where we can add or remove items, we need to use a list. For example, we might want to create a groceryList which could have any number of items in it. A list is a collection, usually of a single data type (like string or int). Unlike an array, a list can dynamically change in length.

Lists


Creating Lists

Let’s open up the REPL and create a new list:

> List<string> groceryList = new List<string> {};

> groceryList
List<string>(0) { }

There's several steps to creating a C# list:

  1. Declare a List. We use the keyword List to create a List object. Notice this is capitalized, unlike data types like string or int.

  2. Declare the data type it will contain. It's best practice to declare the data type in angle brackets after List as we do with List<string> in the example above.

  3. Give it a variable name. In the example above, we gave our List the name groceryList.

  4. Create a new instance of List using its constructor. Finally, we create our List using its constructor with the new keyword: new List<string>. The example above states groceryList will be a new List containing strings.

  5. Add data. Note that we use empty curly brackets at the end of the constructor. If we try to run this without those curly brackets, we'll see: error CS1526: A new expression requires an argument list or (), [], or {} after type. There are several ways to initialize lists, and you might see them used interchangeably. We could use curly brackets with starting values inside {"eggs", "milk"}, or empty, like in this example, which just specifies that our list begins with no data. Using parens () functions the same as empty curly brackets, but if you use parens with an int argument (5), you can specify how many indices of a list your computer should prepare room for on creation, before needing to make more room on your disk. That can help with efficiency! Throughout the curriculum and documentation, you will certainly see lists initialized in a number of different ways. Here is some documentation showcasing a few examples.

Adding Content to Lists

Once we've created a List, we can call the Add() method to add items to the List:

> List<string> groceryList = new List<string> {};

> groceryList.Add("spaghetti");

> groceryList
List<string>(1) { "spaghetti" }

> groceryList.Add("tomatoes");

> groceryList
List<string>(2) { "spaghetti", "tomatoes" }

> groceryList.Add("basil");

> groceryList
List<string>(3) { "spaghetti", "tomatoes", "basil" }

> groceryList.Add("meatballs");

> groceryList
List<string>(4) { "spaghetti", "tomatoes", "basil", "meatballs" }
  • In the code above, we create a new List containing strings.

  • Then we call Add() four times to add four separate strings: "spaghetti", "tomatoes", "basil", and "meatballs".

  • We can call groceryList to see its included items.

Accessing Content in Lists

Items in a List have a unique index just like with arrays. We can access an item at a specific index using square brackets:

> groceryList[1]
"tomatoes"

> groceryList[3]
"meatballs"

We can also redefine/overwrite items at specific indexes using similar notation:

> groceryList[1] = "CANDY!";

> groceryList
{ "spaghetti", "CANDY!", "basil", "meatballs" }

Removing Content from Lists

If we check our pantry and realize we already have basil, we can remove it from our grocery list. To remove an item from a List, we'll use the built-in Remove() method:

> groceryList.Remove("basil");

> groceryList
{ "spaghetti", "CANDY!", "meatballs" }

When we call Remove() on an item in a List, C# will return true if the item is found and removed. If the argument is not present in a List, C# will return false.

Creating Lists


> List<string> groceryList = new List<string> {};

> groceryList;
{ }

To create a list:

  1. Declare a List. We use the keyword List to create a List object. This is capitalized.

  2. Declare the data type it will contain.

  3. Give it a variable name.

  4. Create a new instance of List using its constructor.

  5. Add data. The curly brackets {} at the end are required, even if we're not adding anything to our list yet.

Adding Content to a List


Once we've created a List, we can call the Add() method to add items to the List:

> List<string> groceryList = new List<string> {};

> groceryList.Add("spaghetti");

> groceryList
{ "spaghetti" }

> groceryList
{ "spaghetti" }

Accessing Content in Lists

Items in a List have a unique index just like with arrays. We can access an item at a specific index using square brackets:

> groceryList[0]
"spaghetti"

We can also redefine/overwrite items at specific indexes using similar notation:

> groceryList[0] = "CANDY!";

> groceryList
{ "CANDY!" }

Removing Content from Lists

To remove an item from a List, we use the built-in Remove() method:

> groceryList.Remove("CANDY!")

> groceryList
{}

When we call Remove() on an item in a List, C# will return true if the item is found and removed. If the argument is not present in a List, C# will return false.

Lesson 5 of 9
Last updated more than 3 months ago.