Arraylist - C# Programming Exercise

In this exercise, you need to create a string list using the ArrayList class that already exists in the .NET platform. The ArrayList class allows storing items of any data type, making it a flexible data structure for managing dynamic collections. Once the list is created, you will need to display all the items stored in the list using a loop or an appropriate method for displaying.

Then, you will be asked to insert a new item in the second position of the list, which will change the arrangement of the items. After performing the insertion, you will need to display all the items in the list again to verify that the insertion was done correctly and that the list has been updated.

This exercise will help you get familiar with the ArrayList class in .NET and learn how to handle data collections efficiently in C#, as well as perform basic operations like inserting, removing, and displaying items in the list.

 Category

Dynamic Memory Management

 Exercise

Arraylist

 Objective

Create a string list using the ArrayList class that already exists in the .NET platform.

Once created, display all the items stored in the list. Insert a new item in the second place of the list, and then display all the items again to check if the insertion was done correctly.

 Write Your C# Exercise

// Importing the necessary namespace for using ArrayList
using System; // For basic functionalities like Console
using System.Collections; // For using the ArrayList class

class Program
{
    static void Main(string[] args)
    {
        // Creating an ArrayList to store strings
        ArrayList list = new ArrayList();

        // Adding some initial items to the ArrayList
        list.Add("Apple");  // Adding "Apple" to the list
        list.Add("Banana"); // Adding "Banana" to the list
        list.Add("Cherry"); // Adding "Cherry" to the list

        // Displaying all the items in the ArrayList
        Console.WriteLine("Items in the ArrayList:");
        DisplayList(list);

        // Inserting a new item at the second position (index 1)
        list.Insert(1, "Orange"); // "Orange" will be inserted in the second place

        // Displaying the items again to check if the insertion was done correctly
        Console.WriteLine("\nItems after insertion:");
        DisplayList(list);
    }

    // Helper method to display all the items in the ArrayList
    static void DisplayList(ArrayList list)
    {
        // Looping through each item and printing it to the console
        foreach (var item in list)
        {
            Console.WriteLine(item);
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Dynamic Memory Management

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  ArrayList duplicate a text file

    In this exercise, you need to create a program that reads from a text file and stores it to another text file by reversing the order of the lines. This exercise will ...

  •  Unlimited sum

    In this exercise, you need to create a program that allows the user to enter an unlimited amount of numbers. Besides entering numbers, the program should allow the user to e...

  •  ArrayList - Text file reader

    In this exercise, you need to create a basic text file reader that displays 21 lines of text and allows the user to navigate using the up and down arrow keys, and exit using...

  •  Hast Table - Dictionary

    In this exercise, you need to create a dictionary using a hash table. The purpose of this exercise is to practice the implementation of an efficient data struc...

  •  Parenthesis

    In this exercise, you need to implement a function to check if a sequence of open and closed parentheses is balanced. In other words, the function should check if every open...

  •  Mix and sort files

    In this exercise, you need to create a program that reads the contents of two different files, merges them, and sorts them alphabetically. The program should be able to take...