Lists
A List
is another type of collection class. In contrast with an array, a List
will dynamically increase its size as required.
(Some) Useful List Properties & Methods:
There are many properties and methods available in the List
class: here are a few handy ones.
Properties
Capacity
: lets us get or set the number of elements ourList
can contain.Count
: gets the number of elements currently in theList
.- We can also access elements using square brackets, just like with an
Array
.
Methods
Add()
: public method to add objects to theList
.Clear()
: Removes all elements from theList
.Contains()
: Determines whether an element is in theList
.CopyTo()
: public method that copies aList
to a 1-D array.Insert()
: Inserts an element into aList
.Sort()
: sorts elements using the default comparer.
Creating Lists
This creates a list called dogList
that holds references to Dog
objects:
List<Dog> dogList = new List<Dog>();
We can even make a List
containing elements of a built-in type. Here’s what that looks like, including adding, modifying, and removing elements:
List<int> marks= new List<int>();
marks.Add(50); // add a new element with the value 50
marks.Add(75); // add a new element with the value 75
marks.Add(100); // add a new element with the value 100
marks[1] += 5; // increase the value of the element at index 1
marks.RemoveAt(1); // remove the element at index 1