DEV Community

Cover image for C# Arrays
Grant Riordan
Grant Riordan

Posted on

C# Arrays

In previous chapter we learnt about collections (Lists, Stacks, Queues, Dictionaries), and I gave a special mention to Arrays. Why was this, well arrays can store a collection of objects too just like the aforementioned, however we can't add to them as we go along like the others

e.g

var list = new List<string>();
list.Add("Hello World");

//do other code
list.Add("This is another sentance");
Enter fullscreen mode Exit fullscreen mode

With arrays we have to know the length of the array to allocate memory space to it, the length needs to be known at time of instantiation. The contents of an array cannot be changed once created !

We can declare an array in a few different ways, but the common use of the [] square brackets "keyword / syntax" means an array is being declared.

There are a few ways you can declare an array in C#, explicitly or implicitly. For example when I said we need to know the length, this can be implicitly calculated by creating the array at point of instantiation and setting the values like so.

// will create an integer array with 5 values, will output 5 objects with value 0 (int default is 0)
int[] emptyArray = new int[5] 

//will create a string array with 5 string values, output will be the 5 names.
string[] names = {"Grant", "Gary", "Mary", "Cat", "John"};

//this can also be done using alternative syntax of
string[] names = new string[]{"Grant", "Gary", "Mary", "Cat", "John"};

//Both do the same thing
Enter fullscreen mode Exit fullscreen mode

If you've been playing with C# for a while, or have seen some intellisense tips (code helper when you type code in Visual Studio), you may have seen a method called ToArray().

This does exactly what it says on the tin. It converts a collection to an array. Say you had a List<string> but wanted to use it as an array, I dunno to pass it to another method that only accepts an Array. It's simple you

var list = new List<string>{"Hello", "World", "How are you?"};
var convertedArray = list.ToArray();

Enter fullscreen mode Exit fullscreen mode

Now your list is an Array, and gives you access to array methods and properties.


var list = new List<string>{"Hello","World"};
var array = new string[] {"Hello", "World"};

//Get number of objects 
var countList = list.Count();
var countArray = array.Length;
Enter fullscreen mode Exit fullscreen mode

Array vs List

So when would you use an array vs when to use a List. For me I'd usually opt for a List simply because lists in C# are far more easily sorted, searched through, and manipulated than arrays.

However arrays do have a place, e.g 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.

Accessing Arrays is faster than Lists, this is because a List is just a .Net wrapper around an array. That unwrapping process can affect time taken to load records out of the list etc. There are few other reasons, but far too technical for this article. But we're talking milliseconds here, not huge amounts of time, however with large datasets this may be more noticeable.

In Summary

Use an array when you know how much data is going into it, and the data doesn't need to be added / removed / manipulated.

Use a list when you wish to add / remove data to it easily. If you want to perform multiple actions on the data, pick a list as it offers more built-in functionality.

Top comments (0)