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

An array in C# is a collection of elements with the same data types. It is a sequential collection of elements, and every element has a unique index provided by the array to access them. Similarly, ArrayList is also a collection of elements or objects. But it is a non-generic collection and also supports elements of different types. An ArrayList allows dynamic memory allocation, whereas an array has a fixed size. Let’s learn more about Array Vs ArrayList in this article with examples.

Array

An Array in C# is a collection of elements with their unique index to access them. It is a sequential collection and supports only the variables with similar data types. All the elements in the array get stored in the contiguous memory location. The size of the Array is fixed and static. The default value of an element in a numeric array is 0 (Zero). The index of an array always starts from 0. Let’s learn how to create an array and perform basic operations on the elements.

Array Vs ArrayList.cs

using System;

class ArrayExample {
public static void Main(string[] args) {

// creating an array
int[] myArray = new int[4];

// initializing the array
myArray [0] = 5;
myArray [1] = 72;
myArray [2] = 23;
myArray [3] = 56;

//Sort an array
Array.sort(myArray);

// traversing array
Console.WriteLine(“This article discusses the Array Vs ArrayList concept”);
Console.WriteLine(“The final array:”);
for (int i = 0; i < myArray .Length; i++) 
	Console.WriteLine(myArray [i]);
	}
}


Output

This article discusses the Array Vs ArrayList concept

The final array:
5
23
56
72

We created an array in the above code using the new keyword and assigned values to the variables in the array by accessing them through their indexes. Then, we sorted the array values using the sort() method to arrange the values in ascending order by default. Finally, we iterated over the array to display the values, and it gave us the output shown above.

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

ArrayList

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 how to perform these operations on an ArrayList through the following example.

Array Vs ArrayList.cs

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(“This article discusses the Array Vs ArrayList concept”);
Console.WriteLine("The final ArrayList:");
foreach (var item in myList)
     Console.WriteLine(item);
	}
}


Output

This article discusses the Array Vs ArrayList concept

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.

Now that we have discussed array and ArrayList with example codes, let’s discuss the concept of Array Vs ArrayList to help you understand the difference and similarities between them better

Array Vs ArrayList

Array ArrayList
Array can store only elements of the same data types. ArrayList can store elements of different data types.
Arrays cannot accept null values ArrayList can accept null values.
Arrays are available in System.Array namespace ArrayLists are available in System.Collections namespace
Array has static and fixed-size. ArrayList allows the dynamic size and memory allocation.
Arrays are strongly typed. ArrayLists are not strongly typed.
Insertion and deletion operations are faster than ArrayList Insertion and deletion operations are slower than Array.

 

You can also check out  Difference Between Array List and Linked List in detail.

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.

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 Array or 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.

Which one is faster array or ArrayList in C#?

The array is faster than ArrayList because it has a fixed size. So it becomes easier to perform operations on an array than on an ArrayList. 

What is the default size of ArrayList?

The default size of an ArrayList in C# is 10(ten). Whenever an ArrayList is created, it is of a default size of 10, and the size gets increased on adding more variables into the ArrayList.

Also Read About - singleton design pattern in c# 

Key Takeaways

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

Recommended problems -

 

Hey Ninjas! We hope this blog helped you better understand the Array Vs 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.
Array
3.
ArrayList
4.
Array Vs ArrayList
5.
FAQs
5.1.
What is an ArrayList in C#?
5.2.
Why is ArrayList better than an array?
5.3.
What does the method sort() do in Array or ArrayList?
5.4.
Which one is faster array or ArrayList in C#?
5.5.
What is the default size of ArrayList?
6.
Key Takeaways