C# Arrays vs. Lists

In C#, arrays and lists are both objects that can be used to hold variables, but they aren’t exactly interchangeable.

In C#, an array must be declared using brackets and must be accompanied by the type of variables it will hold (integers or strings) and by its name. To declare an array of integers with the name numbers, it would need to look like this:

int[] numbers;

(you’d need to add a comma within the brackets if you’d like the array to be multidimensional).

To actually create the array, you’d have to add some code to instantiate it, like this:

int[] numbers = new int[];

To create a list in C#, you need to call the list, put the type of list (again, integer or string) in angled brackets, and then follow that with the name of your list. That will declare a new list. Like an array, to actually create a list, you must instantiate it, as seen below:

List<string> Food = new List<string>();

In general, it’s better to use lists in C# because lists are far more easily sorted, searched through, and manipulated in C# than arrays are because of all of the built-in list functionalities in the language. Lists are used far more often in C# than arrays are, but there are some instances where arrays can (or should) be used, including if your data is unlikely to grow very much or if you’re dealing with a relatively large amount of data that will need to be indexed into often.