Data Structures in C# - Creating Custom Collection using ArrayList - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Thursday, July 9, 2026

Data Structures in C# - Creating Custom Collection using ArrayList

Data Structures in C# - Creating Custom Collection using ArrayList

Screenshot from the tutorial
Screenshot from the tutorial

Creating Custom Collections in C# Using ArrayList

In the world of programming, data structures play a crucial role in how we store and manipulate data. In this blog post, we will explore how to create a custom collection in C# using the ArrayList class. This tutorial is based on the YouTube video "Data Structures in C# - Creating Custom Collection using ArrayList," which provides a concise and informative overview of the topic.

What is an ArrayList?

The ArrayList class in C# is part of the System.Collections namespace. It is a dynamic array that can grow and shrink in size, allowing you to store a collection of objects without defining a fixed size in advance. Unlike arrays, ArrayList can store items of different data types, which makes it quite flexible.

Key Features of ArrayList:

  • Dynamic resizing
  • Stores objects of any type (reference types and value types)
  • Provides methods to add, remove, and manipulate items

Setting Up Your Environment

To follow along with this tutorial, ensure you have the following:

  • Visual Studio or any IDE that supports C#
  • Basic understanding of C# syntax and object-oriented programming concepts

Creating a Custom Collection

Let’s create a custom collection using the ArrayList. This collection will store instances of a simple class called Student. Here’s how to do it:

Step 1: Define the Student Class

First, we need to create a simple class to represent a student. This class will have properties such as Id, Name, and Age.

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }

    public Student(int id, string name, int age)
    {
        Id = id;
        Name = name;
        Age = age;
    }

    public override string ToString()
    {
        return $"ID: {Id}, Name: {Name}, Age: {Age}";
    }
}

Step 2: Create the Custom Collection Class

Now, we will create a custom collection class that utilizes an ArrayList to store Student objects.

using System;
using System.Collections;

public class StudentCollection
{
    private ArrayList students = new ArrayList();

    public void AddStudent(Student student)
    {
        students.Add(student);
    }

    public void RemoveStudent(Student student)
    {
        students.Remove(student);
    }

    public void DisplayStudents()
    {
        foreach (Student student in students)
        {
            Console.WriteLine(student);
        }
    }
}

Step 3: Using the Custom Collection

Finally, we can utilize our StudentCollection class in our Main method.

class Program
{
    static void Main(string[] args)
    {
        StudentCollection studentCollection = new StudentCollection();

        // Adding students
        studentCollection.AddStudent(new Student(1, "Alice", 20));
        studentCollection.AddStudent(new Student(2, "Bob", 22));
        studentCollection.AddStudent(new Student(3, "Charlie", 19));

        // Displaying students
        Console.WriteLine("Students in the collection:");
        studentCollection.DisplayStudents();

        // Removing a student
        studentCollection.RemoveStudent(new Student(2, "Bob", 22));

        // Displaying students after removal
        Console.WriteLine("\nStudents after removal:");
        studentCollection.DisplayStudents();
    }
}

Conclusion

In this tutorial, we explored how to create a custom collection in C# using the ArrayList class. We defined a Student class and created a StudentCollection that allows us to add, remove, and display students. This approach demonstrates the flexibility of using ArrayList for dynamic data storage.

While ArrayList is useful, it's worth noting that in modern C#, you might want to consider using generic collections like List<T> for type safety and performance improvements. However, understanding ArrayList is an excellent way to grasp the fundamentals of collections in C#.

Feel free to expand upon this custom collection and implement additional features, such as searching or sorting students!

Happy coding!

Another screenshot from the tutorial
Another view from the tutorial

Connect with SkillBakery Studios

Explore more tutorials, tools, and resources:

Posted by SkillBakery Studios

No comments:

Post a Comment

Post Top Ad