ArrayList And List In C#



ArrayList & List

ArrayList 

ArrayList Class. Advertisements. It represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. However, unlike array you can add and remove items from a list at a specified position using an index and the array resizes itself automatically.

Arraylist removes the fixed-size limitation of array.

ArrayList myList = new ArrayList();
myList.Add(100);
myList.Add(300);
foreach (int myNumber in myList)
{
Console.WriteLine(myNumber);
}

List

Lists in C# are very similar to lists in Java. A list is an object which holds variables in a specific order. The type of variable that the list can store is defined using the generic syntax. Here is an example of defining a list called numbers which holds integers.

List<int> numbers = new List<int>();

The difference between a list and an array is that lists are dynamic sized, while arrays have a fixed size. When you do not know the amount of variables your array should hold, use a list instead.

Once the list is initialized, we can begin inserting numbers into the list.

List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);

Post a Comment

Previous Post Next Post