Arraylist in c# with example

Arraylist in c# with example


ArrayList in C# with Example

In C#, the ArrayList class is a part of the System.Collections namespace and provides a way to store a dynamic array of objects. Unlike standard arrays, ArrayList can dynamically increase and decrease in size as needed. This article explores how to use the ArrayList class effectively, illustrating its flexibility and convenience for managing collections of objects.

Understanding ArrayList

ArrayList is a non-generic collection that can store items of any type, which are internally stored as Object. This means it can hold a mix of different types in the same collection. It is particularly useful in scenarios where the number or types of data elements are not known in advance and can change dynamically.

Key Features of ArrayList

  • Dynamic resizing: Automatically adjusts its size as you add or remove items.
  • Flexibility: Can store elements of any data type since all types are derived from the Object base class.
  • Accessibility: Provides methods to access, sort, search, and manipulate lists.

Example: Basic Operations in ArrayList

Here’s how to perform basic operations such as adding, removing, and accessing items in an ArrayList:

 

ArrayList myList = new ArrayList();

// Adding elements
myList.Add(1);
myList.Add("Two");
myList.Add(3.0);

// Accessing elements
Console.WriteLine("First element: " + myList[0]);  // Outputs 1

// Removing elements
myList.Remove("Two");  // Removes the element "Two"

// Iterating over an ArrayList
foreach (object obj in myList)
{
    Console.WriteLine(obj);
}

ArrayList Methods and Properties

  • Add(object value): Adds an item to the end of the ArrayList.
  • Remove(object value): Removes the first occurrence of a specific object from the ArrayList.
  • Insert(int index, object value): Inserts an element into the ArrayList at the specified index.
  • Count: Gets the number of elements actually contained in the ArrayList.
  • Capacity: Gets or sets the number of elements that the ArrayList can contain.

Example: Sorting and Searching

ArrayList also supports methods for sorting and searching, which can be highly useful for managing ordered data:

 

ArrayList ages = new ArrayList() { 25, 45, 35, 85, 20 };

// Sorting
ages.Sort();
foreach (int age in ages)
{
    Console.WriteLine(age);
}

// Searching
int index = ages.BinarySearch(35);
Console.WriteLine("Index of 35: " + index);

When to Use ArrayList

While ArrayList offers flexibility and dynamic resizing, it lacks type safety, which can lead to runtime errors if types are incorrectly assumed or cast. With the advent of generics in later versions of .NET, List<T> is often a better choice for most applications because it offers type safety and better performance.

However, ArrayList can still be useful in scenarios where you need to store objects of unknown or varying types, or when interacting with APIs that require ArrayList.

Conclusion

ArrayList provides a versatile way to work with lists in C#, where the types and quantities of data are not fixed. It offers methods that facilitate easy manipulation of list data, although developers should be cautious of its type-unsafe nature and prefer more modern alternatives like List<T> when applicable.


 

Leave a reply Your email address will not be published. Required fields are marked*

Categories Clouds