Arrays vs Lists in C#

Elnur
Star Gazers
Published in
4 min readOct 24, 2020

--

Introduction

Arrays and lists are the most used data structures in C# programming language. Both of them store data like integer, string, objects, and so on. They seem very similar at first glance, however they have crucial differences, which affect their usages. Depends on the situation we can prefer to arrays than lists or vice versa. In this article, I am going to explain, which one is better based on what we want to do.

Arrays

Initially, let’s look at the array’s definition:

An array is the data structure that stores a fixed number of literal values of the same type. If we want an array with the different types in it, you can define “object” as its type (object type is not preferable, because they are complex).

Array Declaration

Arrays can be declared by specifying their type and size. Assume, we want an array of integers with the size of 5. We can declare this kind of an array as follows:

int[] myArray;

To create an array we need to initialize it. We do it using new keyword:

int [] myArray = new int[5];

Additionally, you can initialize array elements when you declare it:

int[] myArray = {2,15,36};

Then, we can initialize its items as follows:

myArray[0] = 2;  // Assign 2 to the myArray[0]myArray[1] = 15; // Assign 15 to the myArray[1]myArray[2] = 36; // Assign 36 to the myArray[2]

Advantages and Disadvantages

So, as we grasped some basics of the arrays, let’s look at their advantages and disadvantages:

Advantages

  • Arrays are strongly typed, meaning you can only have one type an element in an array (except object array). This feature gives us 2 advantages:

1.Firstly, the performance will be better, because boxing and unboxing will not occur.

2.Secondly, runtime errors caused by type mismatching will not happen. These kinds of errors are commonly seen while using Lists, Stacks, Queues, etc. For example:

using System;
using System.Collections;

namespace Test
{
class Program
{
static void Main(string[] args)
{
Stack stack = new Stack();

stack.Push(5);
stack.Push("Typing");

foreach (int item in stack)
{
Console.WriteLine(item);
}
}
}
}

In this code, we declared a stack, then we pushed “5” and “Typing” to the stack. In the foreach loop when we wanted to print item which is int type, we will get InvalidCastException error because of type mismacthing.

But if we do the same things on the array, it will not compile:

using System;
using System.Collections;

namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[5];

array[0] = 5;
array[1] = "Typing";
}
}
}
  • Its elements can be accessed by using their indexes.
  • Iterating the arrays using their index is faster compared to any other methods like Linked List etc.

Disadvantages

  • The size of the array should be known before we initialize its elements. Therefore, if we want to add elements more than its size, it will not compile.
  • Insertion and deletion are quite difficult in an array because they are stored in consecutive memory locations. To insert and delete an item would need to shifting operation which is costly.

Lists

Firstly, let’s see its definition:

The List<T> is the collection of objects that can be accessed by index and have special methods for adding, removing, searching, and sorting elements.

List Declaration

The List<T> is dynamic data structures, meaning we do not need to know the number of elements when we declare it. Basically, the list of integers can be declared like this:

List<int> myList;

To use this list, we need to instantiate it:

myList = new List<int>();

Note: We used int variable type, but do not forget that we can use other variable types too

Unlike the arrays, List<T> has its own methods which are very useful.

Add() Method:

You can add new elements to the list using the Add() method:

myList.Add(2); myList.Add(15);myList.Add(36);

If we want to print its elements, we would use the foreach loop:

foreach(int item in myList) 
{
Console.WriteLine(item);
}

Output:

2
15
36

Remove() Method

You can remove items in the list using Remove() method:

myList.Remove(15);

Contains() Method

This method helps us to search for a particular item in an array. It returns true if that item exists:

if(myList.Contains(36))
{
Console.WriteLine("36 exists in the List");
}

Advantages and Disadvantages

Advantages

  • List size can be scaled up after declaration.
  • Like an array, you can access its elements by indexes.
  • It has useful methods like Add() and Remove().

Disadvantages

  • Lists occupy more memory than arrays.

Conclusion

In conclusion, If you have a fixed size collection, then it would be much better to use arrays. For example, you have a string array called names with the names of 5 people. On the other hand, if your collection has to be dynamic, then use lists. For example, you create a list but you will fill it later. In this case, lists are more useful. So, we can say that both arrays and lists are being really useful when they are used correctly.

--

--