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.
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:
Declare a List
. We use the keyword List
to create a List
object. Notice this is capitalized, unlike data types like string
or int
.
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.
Give it a variable name. In the example above, we gave our List
the name groceryList
.
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 string
s.
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.
List
sOnce 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 string
s.
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.
List
sItems 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" }
List
sIf 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
.
> List<string> groceryList = new List<string> {};
> groceryList;
{ }
To create a list:
Declare a List
. We use the keyword List
to create a List
object. This is capitalized.
Declare the data type it will contain.
Give it a variable name.
Create a new instance of List
using its constructor.
Add data. The curly brackets {}
at the end are required, even if we're not adding anything to our list yet.
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" }
List
sItems 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!" }
List
sTo 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.