Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C# ArrayList: Comprehensive Tutorial with Practical Examples

What is ArrayList in C#?

The ArrayList collection, part of System.Collections, behaves like a traditional array but offers dynamic resizing. Unlike static arrays, you don’t need to declare a fixed size upfront; elements can be added or removed at any time.

Declaring an ArrayList

Instantiate an ArrayList using the new keyword and assign it to a variable. This variable becomes the handle through which you access and manipulate the collection.

ArrayList a1 = new ArrayList();

Adding Elements

The Add method appends any object to the list, whether it’s an int, string, bool, or a custom type.

ArrayList.Add(element)

Examples:

Below is a complete console program that demonstrates creation, addition, and retrieval of elements:

Mastering C# ArrayList: Comprehensive Tutorial with Practical Examples

using System;
using System.Collections;

namespace DemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList a1 = new ArrayList();
            a1.Add(1);
            a1.Add("Example");
            a1.Add(true);

            Console.WriteLine(a1[0]);   // 1
            Console.WriteLine(a1[1]);   // Example
            Console.WriteLine(a1[2]);   // True
            Console.ReadKey();
        }
    }
}

Explanation:

  1. Instantiate the ArrayList.
  2. Use Add to insert elements of varying types.
  3. Access items by index, starting at 0.

Running this code produces:

Mastering C# ArrayList: Comprehensive Tutorial with Practical Examples

Common ArrayList Methods

Count

Retrieves the number of elements currently stored.

int total = a1.Count;

Contains

Checks if a specified element exists in the list.

bool exists = a1.Contains(2);

RemoveAt

Deletes the element at the given zero‑based index.

a1.RemoveAt(1);

The following program showcases these methods in action:

Mastering C# ArrayList: Comprehensive Tutorial with Practical Examples

using System;
using System.Collections;

namespace DemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList a1 = new ArrayList();
            a1.Add(1);
            a1.Add("Example");
            a1.Add(true);

            Console.WriteLine(a1.Count);          // 3
            Console.WriteLine(a1.Contains(2));    // False
            Console.WriteLine(a1[1]);             // Example
            a1.RemoveAt(1);                      // Remove "Example"
            Console.WriteLine(a1[1]);             // True
            Console.ReadKey();
        }
    }
}

Explanation:

  1. Display the total number of items.
  2. Check for the presence of value 2.
  3. Show the element at index 1 before removal.
  4. Remove the element at index 1.
  5. Display the new element at index 1 (now True).

The output illustrates the dynamic nature of the collection:

Mastering C# ArrayList: Comprehensive Tutorial with Practical Examples

Summary

ArrayList provides a flexible, non‑generic container for heterogeneous data. While modern C# favors List<T> for type safety, understanding ArrayList remains valuable for legacy codebases and quick prototyping.

C Language

  1. Mastering Java ArrayList: Operations, Methods, and Best Practices
  2. C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops
  3. Master C# Arrays: Declaring, Initializing, and Working with Integer Collections
  4. Master C# Classes & Objects: A Practical Tutorial with Real‑World Examples
  5. Master C# Collections: A Practical Tutorial with Real-World Examples
  6. C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples
  7. Understanding Switch‑Case in C: Syntax, Examples, and Best Practices
  8. Java Multithreading Explained: Concepts, Lifecycle, and Practical Code Examples
  9. Python Arrays: Creation, Manipulation, and Advanced Operations – Expert Guide
  10. Comprehensive PyQt5 Tutorial: Build Professional GUIs in Python