A typical problem with array types is fixed capacity. If you don't know beforehand how many objects the array will hold, you risk a space that is too small (overflow) or too large (a waste of space) to declare an array.
Your program may allow users to enter data or collect data from a Web site. When it finds objects (strings, books, values, and so on), they are added to the array, but you do not know how much data will be collected during this period of time. Fixed-size arrays are not a good choice, because you don't know how large an array is needed.
The list class is an array of dynamically increasing dimensions as needed. It provides a set of useful methods and properties for operations. The most important of them is shown in table 9-3.
Table 9-3 list methods and properties
Method or Property |
Role |
Capacity |
Used to get or set the number of elements that the list can hold. This value is automatically increased when the quantity exceeds the capacity. You can set this value to reduce capacity, or you can call the Trin () method to reduce the capacity to fit the actual number of elements. |
Count |
Property that is used to get the current number of elements in the array |
Item () |
Gets or sets the element by specifying the index. For the list class, it is an indexer. |
ADD () |
To add a public method of an object to the list |
AddRange () |
Public method, adding multiple elements that implement the ICollection interface at the end of the list |
BinarySearch () |
Overloaded public method used to locate the specified element using a binary lookup within the sorted list. |
Clear () |
Remove all elements from the list |
Contains () |
Test whether an element is in the list |
CopyTo () |
Overloaded public method, copy a list to a one-dimensional array |
Exists () |
Test whether an element is in the list |
Find () |
Finds and returns the first occurrence of a matching element in a list |
FindAll () |
Finds and returns all matching elements within a list |
GetEnumerator () |
Overloaded public method, returns an enumerator for the iteration list |
GetRange () |
Copy the specified range of elements into the new list |
IndexOf () |
Overloaded public method that finds and returns the index of each matching element |
Insert () |
Inserts an element into the list |
Insertrange () |
Insert a set of elements within a list |
LastIndexOf () |
Overloaded public method, to find and return the index of the last matching element |
Remove () |
Removes the first element that matches the specified element |
RemoveAt () |
To remove an element from a specified index |
RemoveRange () |
Removes the specified range of elements |
Reverse () |
Reverses the order of elements within a list |
Sort () |
Sort the elements in a list |
ToArray () |
Copy the elements in the list into a new array |
TrimToSize () |
Set capacity to actual number of elements in list |