Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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.
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.
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.