π Lists
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> {};
There's several steps to creating a C# list. From left to right:
Declare a
List
. We use the keywordList
to create aList
object. Notice this is capitalized, unlike data types likestring
orint
.Declare the data type the List will contain. It's best practice to declare the data type in angle brackets after
List
as we do withList<string>
in the example above.Give it a variable name. In the example above, we gave our
List
the namegroceryList
.Create a new instance of
List
using its constructor. Finally, we create ourList
using its constructor with thenew
keyword:new List<string>
. The example above statesgroceryList
will be a newList
containingstring
s.Add data. Note that we use empty curly brackets at the end of the constructor to initialize an empty list without any data. 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
.
Note that there are several ways to initialize lists, and you might see them used interchangeably. We could use curly brackets with starting values inside like this:
List<string> groceryList2 = new List<string> {"eggs", "milk"};
Or, you might see parens used ()
instead of curly brackets {}
to initialize an empty list:
List<string> groceryList3 = new List<string> ();
And there's still a few other ways to initialize a list. Dot Net Perls has helpful documentation on this topic with some more examples.
Adding Content to List
sβ
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
List<string>(0) { }
> groceryList.Add("spaghetti");
> groceryList.Add("tomatoes");
> groceryList.Add("basil");
> groceryList.Add("meatballs");
> groceryList
List<string>(4) { "spaghetti", "tomatoes", "basil", "meatballs" }
In the code above, we create a new
List
ofstring
s.Then we call
Add()
four times to add four separate strings:"spaghetti"
,"tomatoes"
,"basil"
, and"meatballs"
.We call
groceryList
to see its included items before and after we're done adding items.
Accessing Content in List
sβ
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 List
sβ
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
.
The List<T>
Classβ
We can learn more about lists by reviewing the information in the List<T>
class. There are many helpful methods and properties! For example, check out this instance method called Insert()
that lets us insert a new element at a specified index:
> List<string> groceryList2 = new List<string> { "spaghetti", "tomatoes", "basil", "meatballs" };
> groceryList2.Insert(0, "flowers")
1