Arrays vs Lists in C#

Elnur
Elnur
Oct 24 · 4 min read
Image for post
Image for post

Introduction

Arrays and Lists are the most used data structures in C# programming language. Both of them store data like integer, string, instances of the class, and so on. They seem very similar at first glance, but actually, they have some necessary differences which affect their usages. Depends on the situation we can prefer to Arrays than Lists or vice versa. In this blog, 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 5. We can declare this kind of an array as follows:

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

To create an array, you need to instantiate it using new keyword:

Then, we can initialize its items as follows:

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.

  • 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.
  • It only allows you to have a fixed size of elements. Once, we declared an array, we can not change its size. 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:

To use this list, we need to instantiate it:

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:

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

Output:

Remove() Method

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

Contains() Method

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

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.
  • Application performance will be slower in terms of speed

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.

The Startup

Medium's largest active publication, followed by +731K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store