Well done! You’ve learned a lot of useful information in this lesson:
List<T>, is a generic, sequential data structure. The specific type that it contains is specified upon instantiation.
Add()is used to add an element to a list.
Remove()is used to remove an element from the list. It returns
trueif it is successful,
Countis the number of elements in the array.
trueif the argument exists in the list,
If you ever forget the constructors, properties, and methods taught here, you can find them in the Microsoft documentation for
Finally, you may be wondering: if lists have so many great features, why did we bother with arrays at all?
The answer is (this an optional part of the lesson): under the hood, lists actually use arrays! When we construct a list, the C# compiler constructs an array and stores the elements there. If the list gets longer than the array’s length, the compiler copies the list elements to a new, longer array. To the developer, it just looks like the list is infinitely long.
Thus arrays are faster to use when you have a pre-determined number of elements, and lists are better to use when the number is unknown or you’d like the extra features. If you’d like to dive deeper into this concept, start with the
Add your city to the list, then use a
foreach loop to print the whole list to the console.