C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we'll demonstrate how to use the ArrayList
class in C#. The ArrayList
class is a dynamic array, which means that its size can be changed during runtime. It is part of the System.Collections
namespace.
Create a new C# Console Application project in Visual Studio and add the following namespace:
using System.Collections;
Create an instance of the ArrayList
class:
ArrayList myArrayList = new ArrayList();
You can use the Add
method to add elements to the ArrayList:
myArrayList.Add(10); myArrayList.Add("Hello"); myArrayList.Add(3.14);
Note that the ArrayList can store different types of elements, unlike an array.
To access elements in the ArrayList, use the indexer property and provide the index:
int firstElement = (int)myArrayList[0]; string secondElement = (string)myArrayList[1];
Note that you need to cast the elements to their appropriate types since the ArrayList stores elements as objects.
Use the Insert
method to insert an element at a specified index:
myArrayList.Insert(1, "World");
Use the Remove
method to remove the first occurrence of an element:
myArrayList.Remove(3.14);
Use the RemoveAt
method to remove an element at a specified index:
myArrayList.RemoveAt(2);
Use a foreach
loop to iterate through the elements of the ArrayList:
foreach (object item in myArrayList) { Console.WriteLine($"Element: {item}"); }
The ArrayList class has several useful properties and methods, such as Count
, Contains()
, and IndexOf()
. For example:
int count = myArrayList.Count; // Returns the number of elements in the ArrayList bool contains = myArrayList.Contains("Hello"); // Returns true if the ArrayList contains the specified element int index = myArrayList.IndexOf("Hello"); // Returns the index of the first occurrence of the specified element in the ArrayList
In this tutorial, we've shown you how to use the ArrayList
class in C#. The ArrayList
class is a dynamic array that can store elements of different types and resize itself during runtime. However, it is worth noting that the ArrayList
class is not type-safe and has some performance overhead due to boxing and unboxing of value types. In most cases, it is recommended to use the generic List<T>
class from the System.Collections.Generic
namespace, which provides type safety and better performance.
C# ArrayList example:
ArrayList
class in C#, demonstrating its use as a dynamic array that can store elements of different types.// Create and initialize an ArrayList ArrayList arrayList = new ArrayList(); arrayList.Add(1); arrayList.Add("Hello"); arrayList.Add(3.14); // Access and print elements of the ArrayList foreach (var element in arrayList) { Console.WriteLine(element); }
How to declare and initialize ArrayList in C#:
ArrayList
in C#.// Declare and initialize an ArrayList ArrayList arrayList = new ArrayList(); arrayList.Add(1); arrayList.Add(2); arrayList.Add(3);
Accessing elements in an ArrayList in C#:
ArrayList
using index notation.ArrayList arrayList = new ArrayList(); arrayList.Add("Apple"); arrayList.Add("Banana"); // Access and print specific elements Console.WriteLine(arrayList[0]); // Output: Apple Console.WriteLine(arrayList[1]); // Output: Banana
Iterating through an ArrayList in C#:
ArrayList
in C#, such as using foreach
and traditional for
loops.ArrayList arrayList = new ArrayList(); arrayList.Add(1); arrayList.Add(2); arrayList.Add(3); // Using foreach loop foreach (var element in arrayList) { Console.WriteLine(element); } // Using for loop for (int i = 0; i < arrayList.Count; i++) { Console.WriteLine(arrayList[i]); }
Adding and removing elements from ArrayList in C#:
ArrayList
in C#.ArrayList arrayList = new ArrayList(); arrayList.Add(1); arrayList.Add(2); arrayList.Add(3); // Add a new element arrayList.Add(4); // Remove an element arrayList.Remove(2);
Sorting an ArrayList in C#:
ArrayList
in ascending order using the ArrayList.Sort
method.ArrayList arrayList = new ArrayList(); arrayList.Add(5); arrayList.Add(3); arrayList.Add(1); // Sort the ArrayList in ascending order arrayList.Sort(); // Print the sorted ArrayList foreach (var element in arrayList) { Console.WriteLine(element); }
Searching for an element in an ArrayList C#:
ArrayList
, such as using ArrayList.IndexOf
.ArrayList arrayList = new ArrayList(); arrayList.Add("Apple"); arrayList.Add("Banana"); arrayList.Add("Orange"); // Find the index of the target element int index = arrayList.IndexOf("Banana"); // Print the index or a message if not found Console.WriteLine(index != -1 ? $"Element found at index {index}" : "Element not found");
Passing ArrayList as parameters in C#:
ArrayList
as parameters to methods in C#, enabling modular and reusable code.// Method that takes an ArrayList as a parameter static void PrintArrayList(ArrayList list) { foreach (var element in list) { Console.WriteLine(element); } } // Declare and initialize an ArrayList ArrayList arrayList = new ArrayList(); arrayList.Add(1); arrayList.Add(2); arrayList.Add(3); // Invoke the method with the ArrayList as a parameter PrintArrayList(arrayList);