Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

ArrayList is an ordered and non-generic collection of elements in C#. Every element has its unique index and a better alternative to the arrays. ArrayList allows dynamic memory allocation. It also allows adding, deleting, and updating the elements dynamically. ArrayList supports the addition of elements with unknown data types and sizes. We can also use ArrayList to search and sort the elements. Let’s learn about the properties and methods of ArrayList now.

Properties of ArrayList

Property Description
Capacity Defines the number of elements an ArrayList can contain.
Count Stores the actual number of elements present in ArrayList.
Item Stores the element at a specified index in the ArrayList.
SyncRoot Defines an object used to synchronize the access to ArrayList.
IsFixedSize A boolean value indicating whether the ArrayList has a fixed size.
IsReadOnly A boolean value indicating whether the ArrayList is read-only.
IsSynchronized A boolean value indicating whether the ArrayList is synchronized.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Methods of ArrayList

Method Description
Add(object value) This method adds an object to the end of an ArrayList.
AddRange(ICollection c) This method adds the elements of an ICollection to the end of an ArrayList
Clear() This method removes all the elements from an ArrayList.
Contains(object item) This method checks whether an element is present in the ArrayList.
GetRange(int index, int count) This method returns a subset of ArrayList within the specified range.
IndexOf(object) This method returns the index of the first occurrence of an element in the ArrayList.
Insert(int index, object value) This method inserts an element at the specified index into the ArrayList.
InsertRange(int index, ICollection c) This method inserts a collection of elements at the specified index into the ArrayList
Remove(object obj) This method removes the first occurrence of an object from the ArrayList.
Reverse() This method reverses all the elements in the ArrayList.
Sort() This method sorts the elements in the ArrayList
TrimToSize() This method sets the capacity to the actual number of elements in the ArrayList.

According to your code, all the methods listed above have the access specifier public and the virtual keyword with any data type.

ArrayList operations

ArrayList allows us to perform different operations like

  • Create ArrayList
  • Adding elements to ArrayList
  • Removal of elements from the ArrayList
  • Iterate over an ArrayList
     
using System;
using System.Collections;
using System.Collections.Generic;

class ArrayListExample {

public static void Main()
{
// Creating an ArrayList
ArrayList myList = new ArrayList();

// Adding elements to ArrayList
myList.Add("Mango");
myList.Add("Banana");
myList.Add("Kiwi");

//Remove an element at index 1
myList.RemoveAt(1);

//Iterate over the ArrayList
Console.WriteLine("The final ArrayList:");
foreach (var item in myList)
     Console.WriteLine(item);
}
}


Output

The final ArrayList:

Mango

Kiwi

We created a new ArrayList in the above code and added the elements; Mango, Banana, and Kiwi. We removed the element at index 1(Banana) in the ArrayList. After iterating over the final ArrayList, we got the output given above.

Check out this array problem - Merge 2 Sorted Arrays

Must Read Difference between ArrayList and LinkedList

FAQs

What is an ArrayList in C#?

ArrayList is an ordered and non-generic collection of elements in C#. Every element has its unique index and a better alternative to the arrays. ArrayList allows dynamic memory allocation. It also allows adding, deleting, and updating the elements dynamically.

Which is better, list or ArrayList in C#?

List saves us from the run-time errors and casting overhead caused by the ArrayList in C#. They are easy to manage and support different data types. So Lists are preferred more than ArrayList.

What are the operations supported by ArrayList in C#?
ArrayList allows us to perform different operations like; creating ArrayList, adding elements to ArrayList, removing elements from the ArrayList, and iterating over an ArrayList.

Why is ArrayList better than an array?

ArrayList allows us to allocate the memory dynamically and supports operations like creating, deleting, and inserting elements of different data types into the ArrayList, unlike an array. So an ArrayList is better than an array if you want it to be dynamic.

What does the method sort() do in ArrayList?

By default, the sort() method sorts the elements in an ArrayList in the ascending order or lexicographical order. The sorting of elements depends on the data type of the elements.

Key Takeaways

We have discussed the concept of the ArrayList in C# in this article with clear examples and a code. You can now use the ArrayList in your code. 

Recommended problems -


Hey Ninjas! We hope this blog helped you better understand the ArrayList concept in C#. Please check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Topics covered
1.
Introduction
2.
Properties of ArrayList
3.
Methods of ArrayList
4.
ArrayList operations
5.
FAQs
5.1.
What is an ArrayList in C#?
5.2.
Which is better, list or ArrayList in C#?
5.3.
Why is ArrayList better than an array?
5.4.
What does the method sort() do in ArrayList?
6.
Key Takeaways