How to create the ArrayList in C#?

How to create the ArrayList?

ArrayList is a dynamic array provided by C# as a part of the System.Collections namespace. Unlike regular arrays, ArrayList can grow or shrink in size dynamically.

Let's go through a basic tutorial on how to create and manipulate an ArrayList in C#.

1. Setting Up:

To start, make sure you have .NET SDK installed on your machine.

Create a Console Application:

  • Open a terminal or command prompt.
  • Create a new directory for your project and navigate to it:
mkdir ArrayListDemo cd ArrayListDemo 
  • Create a new console application:
dotnet new console 

This command initializes a new console application and creates a Program.cs file and a .csproj file.

2. Writing the Code:

Open Program.cs in your favorite text editor or IDE.

Add using System.Collections; at the top to use the ArrayList class.

Here's a basic example of using an ArrayList:

using System; using System.Collections; namespace ArrayListDemo { class Program { static void Main(string[] args) { ArrayList list = new ArrayList(); // Add items list.Add("Hello"); list.Add(42); list.Add(DateTime.Now); // Add a range of items int[] numbers = {1, 2, 3, 4, 5}; list.AddRange(numbers); // Display the count and capacity Console.WriteLine($"Count: {list.Count}"); Console.WriteLine($"Capacity: {list.Capacity}"); // Print items foreach (var item in list) { Console.WriteLine(item); } // Remove items list.Remove(42); // Removes the first occurrence of 42 // Check if an item exists bool containsHello = list.Contains("Hello"); Console.WriteLine($"Contains 'Hello': {containsHello}"); // Clear the ArrayList list.Clear(); Console.WriteLine($"Count after clearing: {list.Count}"); } } } 

3. Running the Program:

Back in your terminal or command prompt, compile and run the program:

dotnet run 

You should see the items you added to the ArrayList printed to the console along with other data like the count of items.

Understanding the Code:

  • ArrayList list = new ArrayList();: This creates a new instance of the ArrayList class.

  • list.Add(...): This method is used to add items to the ArrayList.

  • list.AddRange(...): Adds multiple items to the ArrayList.

  • list.Count: Returns the number of items in the ArrayList.

  • list.Contains(...): Checks if the ArrayList contains a specific item.

  • list.Remove(...): Removes the first occurrence of a specific item from the ArrayList.

  • list.Clear(): Removes all items from the ArrayList.

Note:

While ArrayList is versatile because it can store any type of data, it's not type-safe, meaning you can inadvertently introduce errors by storing the wrong type of data. Modern C# development generally favors generic collections like List<T> from the System.Collections.Generic namespace for better type safety.


More Tags

popen webservices-client browser onscrolllistener jsonresult scjp amazon-dynamodb apache-stanbol biometrics mbstring

More Programming Guides

Other Guides

More Programming Examples