C# ArrayList Class

ArrayList Class

The ArrayList class is part of the System.Collections namespace and represents a dynamically sized collection of objects. Unlike arrays, which have a fixed size, the ArrayList can grow or shrink automatically. Here's a tutorial on how to use the ArrayList class in C#.

1. Introduction

ArrayList is a non-generic collection, meaning it can store any object type. This provides flexibility but can lead to type-safety issues. It's recommended to use generic collections like List<T> in modern C#, but understanding ArrayList is still valuable for both legacy code and foundational knowledge.

2. Adding Elements

To add elements to an ArrayList, you can use the Add method:

using System; using System.Collections; public class Program { public static void Main() { ArrayList list = new ArrayList(); list.Add(1); list.Add("Two"); list.Add(3.0); foreach (var item in list) { Console.WriteLine(item); } } } 

3. Removing Elements

You can remove elements by value or by index:

list.Remove("Two"); // Removes the first occurrence of "Two" list.RemoveAt(1); // Removes the element at index 1 

4. Other Useful Methods

  • Insert: Inserts an element into the ArrayList at a specific index.

    list.Insert(1, "Inserted"); 
  • Contains: Determines whether an element is in the ArrayList.

    bool exists = list.Contains("Two"); 
  • IndexOf: Returns the zero-based index of the first occurrence of a value in the ArrayList or a portion of it.

    int index = list.IndexOf("Two"); 
  • Sort: Sorts the elements in the entire ArrayList.

    list.Sort(); 
  • Reverse: Reverses the order of the elements in the entire ArrayList.

    list.Reverse(); 

5. Accessing Elements

You can access elements by index. Since the ArrayList stores objects, you might need to cast the retrieved object to its original type:

int firstElement = (int)list[0]; 

6. Limitations and Type Safety

Since ArrayList can store any object, it's not type-safe. This can lead to runtime errors:

list.Add("A string"); int willFail = (int)list[3]; // This will throw an InvalidCastException at runtime 

To ensure type safety, you should use generic collections like List<T>:

using System.Collections.Generic; List<int> intList = new List<int>(); intList.Add(1); intList.Add(2); 

7. Key Points to Remember

  • ArrayList is dynamically sized, and its capacity grows automatically.

  • It belongs to the System.Collections namespace and is not type-safe.

  • Always check for potential type-casting errors when retrieving items from an ArrayList.

  • For type safety and modern practices, prefer using List<T> from the System.Collections.Generic namespace.

Conclusion

While ArrayList provides a dynamic-sized collection, it has largely been superseded by generic collections like List<T> due to their type safety. However, understanding ArrayList remains important for working with older code or when non-generic collections are needed.


More Tags

actionlink instagram ubuntu-16.04 log4net-appender fiddler page-refresh xml-parsing image-editing patindex can-bus

More Programming Guides

Other Guides

More Programming Examples