ArrayList
Explore the ArrayList class in C# and learn how it overcomes the fixed size limitation of arrays by providing dynamic resizing and flexible item management. Understand common ArrayList methods like Add, RemoveAt, Insert, and Contains, along with the challenges related to type safety when storing mixed data types. This lesson helps you grasp the use cases and drawbacks of ArrayList as a dynamic collection in .NET development.
We'll cover the following...
When we need to store multiple items in one place, we can use arrays. We use them to store multiple items of the same type:
int[] points = new int[30]; // Array that can hold 30 integers
We declare an integer array named points and initialize it with a fixed size of 30. We can also iterate an array to process one item at a time:
for (int i = 0; i < points.Length; i++){Console.WriteLine(points[i]);}
Lines 1–4: We use a
forloop to iterate through thepointsarray and print each item to the console.
Arrays have one major shortcoming: we must know how many items the array will hold before creating it. Arrays aren’t dynamic. In a real-life application, however, we may not know how many items we plan to store. ...