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.
-
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 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. The example above states groceryList will be a new List containing strings.
-
Add data. The curly brackets {} at the end are required, even if we're not adding anything to our list yet. We can also add data to this 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.Add("tomatoes");
> groceryList
{ "spaghetti", "tomatoes" }
> groceryList.Add("basil");
> groceryList
{ "spaghetti", "tomatoes", "basil" }
> groceryList.Add("meatballs");
> groceryList
{ "spaghetti", "tomatoes", "basil", "meatballs" }
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.
Dictionaries are a bit like phone books. Just as a phone book stores pairs of names and phone numbers, a dictionary holds key-value pairs. With a phone book, we look up a number (value) by its key (a person's name).
The basic format for creating a dictionary looks like this:
Dictionary<string, string> dictionaryName = new Dictionary<string, string>() {};
Here are all the steps to creating our dictionary:
-
Declare a Dictionary. We start with the keyword Dictionary to inform C# we're creating a Dictionary object. Dictionary should be capitalized, not lowercased like string or int.
-
Determine what the Dictionary will contain. We then declare the type of data the dictionary will contain in angle brackets: Dictionary<string, string>. Because Dictionarys contain key-value pairs, we declare two data types here. The first is the data type keys will be. The second is the data type for values.
-
Give it a variable name. We'll save our Dictionary in a variable so we can store it and access it later. In the example above, we gave our Dictionary the variable name myDictionary.
-
Create a new Dictionary instance with its constructor. Similar to Lists from the last lesson, we use its constructor with the new keyword: new Dictionary<string, string>().
-
Add data. The curly brackets {} at the end are required. They contain the information we'd like to put in our Dictionary. This information is formatted in key-value pairs that are also in curly brackets. If we're creating an empty dictionary, we leave these empty.
Make a new Dictionary:
> Dictionary<string, int> cupcakeOrder = new Dictionary<string, int>() { {"vanilla", 12}, {"chocolate", 24}, {"raspberry", 6}, {"caramel apple", 36} };
In this case, our value is an int instead of a string. Each number is stored with its associated flavor of cupcake. When we get to the bakery, we can easily check how many of each type we need:
> cupcakeOrder["vanilla"] // How many vanilla cupcakes do we want?
12
> cupcakeOrder["chocolate"] // How many chocolate?
24
> cupcakeOrder["raspberry"]
6
> cupcakeOrder["caramel apple"]
36
Make a new dict:
> Dictionary<string, string> myDictionary = new Dictionary<string, string>() { {"A", "apple"}, {"B", "bear"} };
Add a new entry:
> myDictionary["C"] = "cat";