C# List vs. Array

What is the Difference between arrays and lists in C#?

In C#, there are two ways to handle multiple data: arrays and lists. Arrays and lists differ in the conditions used and initialization methods.
Understanding the difference between arrays and lists and how they are initialized will broaden the scope of your project.

Arrays and lists are similar in that they manage multiple data elements, but each has its own unique features. Let's look at the differences and how to initialize them together.

See more C# tutorials:

C# List vs. Array

The difference between an array and a list is that the number of data handled may or may not be fixed.

Arrays are designed to store data of a specific length, and the number of elements is defined when the array is created.

Once initialized, the number of elements cannot be raised or lowered.

Arrays are useful when dealing with fixed-length data, such as the number of days in a week or the number of months in a year.

On the other hand, lists are suitable for handling data of varying lengths. Unlike arrays, the number of elements is not specified at initialization and can be changed after initialization.

Because it supports variable-length data, it is suitable for handling items with changing numbers, such as when you want to manage ledger book information.

Initializing an array in C#

To initialize an array, write as follows:

string[] countries = new string[4];

As explained in the previous section, the number of elements is specified at initialization. In the example above, the number is 4.

The number of elements can only be specified when initializing. It is also possible to set the value at the same time as initialization.

string[] countries = new string[] { "Italy", "Spain", "France", "Korea" };

Or:

string[] countries = { "Italy", "Spain", "France", "Korea" };

This method is often used because the values can be set at the same time as the initialization described above.

Since arrays can store fixed-length data, here is an example:

  1. using System;
  2. namespace CSharpExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. //Initializa an array
  9. string[] countries = { "Italy", "Spain", "France", "Korea" };
  10. //Display the second element
  11. Console.WriteLine("The second element: {0}", countries[1]);
  12. }
  13. }
  14. }
  15.  

Output:

The second element: Spain

Initializing a List in C#

To initialize the List, write as follows:

List<T> list = new List<T>();

Unlike arrays, the number of elements is not specified when initializing but can be changed after initialization.

Lists are ideal for storing data of varying lengths.
Here's an example:

  1. using System;
  2. namespace CSharpExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. //Initializa an empty list
  9. List<string> movies = new List<string>();
  10. //Add some movies
  11. movies.Add("Movie 1");
  12. movies.Add("Movie 2");
  13. movies.Add("Movie 3");
  14. movies.Add("Movie 4");
  15. //Remove
  16. movies.Remove("Movie 3");
  17. //Display the list items
  18. foreach (string movie in movies)
  19. {
  20. Console.WriteLine("Movie: {0}", movie);
  21. }
  22. }
  23. }
  24. }

Output:

Movie: Movie 1
Movie: Movie 2
Movie: Movie 4

It is excellent for managing variable-length data as described above because the number is not fixed at initialization, and additions and deletions are made as the process continues.

By making good use of arrays and lists, you can expand the range of implementation depending on the type of data you are dealing with. Be sure to understand the differences between the two and how to utilize them properly.

Summary

Arrays and lists have different uses depending on the type of data and whether they are fixed or variable length.
The better you can distinguish between arrays and lists, the better you can code when designing your application.

Click the link below to see a list of our C# tutorials:

A list of C# tutorials


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables