Day 6: The Difference Between List and Array

Leslie Ramírez
3 min readDec 13, 2021

--

Hi there👋👋! Hope you’re doing well. This time I’m going to share with you some important information about 2 important types in C#, List and Array. It’s really common that we know how to use certain characteristics or types in our programming language, but we don’t know the details or reason why we should do the things in some ways, so that’s why today I’m going to show you in short the key differences between them.

Let ‘s get started!

First let’s begin with a formal definition of a List.

List<T>

“Represents a strongly typed list of objects that can be accessed by index” This short expression means that a list contains objects, that we can perform actions with it as sort, search and manipulate list. What does the T mean? This represents the type of object of the list in a generic way.

For create our List we need to add the System.Collections.Generic namespace like this:

using System.Collections.Generic;

The basic methods you need to manipulate a List are the following:

These are some of the basic operations that can be made with List for a detailed list of methods, I added you the documentation link at the end of this post. So moving forward to our next element Let’s talk about arrays.

Arrays

Are used to store multiple values of the same type in a single variable. You declare an array by specifying the type of its elements. An array can be single dimensional, multidimensional or jagged.

General characteristics of arrays:

  • Arrays index starts at zero.
  • A jagged array is an array of arrays.
  • An array can be any type, including array type.
  • You can use the foreach statement to iterate through an array.

Some of the basic syntax to work with arrays:

The key Differences between Arrays and List<T>

  • The List<T> class performs better in most cases and is type safe.
  • The number of dimensions and the length of each dimension are established when the array instance is created. These values can’t be changed during the lifetime of the instance.
  • List<T> capacity can be modified at runtime, array size is defined in the creation and cannot be modified during runtime.
  • Arrays are specially optimized for arithmetic computation for that case, it is more convenient to use it instead of a List.

Conclusions

Lists and Arrays are very similar, we can store data in both, they can be sorted, sliced but in practical situations we use a List when we don’t know the size of the elements we will store in the data structure, we don’t have a specific number for the length of the object and maybe It would be dynamic. In the order hand when performing arithmetic computation and we have a specific length for the object we use an array.

If you enjoyed this article, please hold down the clap button 👏 👏 👏Don’t forget to follow our Coding winter Calendar. Have a wonderful day! 🧡 💛 💚🧡 💛 💚

If you want to get more information about all the methods and characteristics of them, follow these links:

List<T> Class (System.Collections.Generic) | Microsoft Docs

--

--

Leslie Ramírez

I am Microsoft MVP in the award category: Developers technologies, a professional with several years in software development specifically in .NET technology.