Arrays are data structures that help programmers store multiple values of a specific type in a single variable. We can use different techniques to initialize arrays in C#, and we are going to discuss them in this article.
Let’s start
Initialize Arrays in C# with Known Number of Elements
We can initialize an array with its type and size:
var students = new string[2];
Here, we initialize and specify the size of the string array. i.e. 2. We can use this technique in situations where we know the number of elements in an array but we don’t know the values.
We can test that the array has a length of 2:
public void GivenArrayOfFixedLength_ThenReturnCorrectLength() { var students = new string[2]; Assert.AreEqual(2, students.Length); }
Of course, if we just want to create an array without elements we can provide 0 for the size, or we can use Array.Emtpy<T>
method:
[TestMethod] public void GivenEmptyArray_ThenReturnCorrectLength() { var students = new string[0]; var students1 = Array.Empty<string>(); Assert.AreEqual(0, students.Length); Assert.AreEqual(0, students1.Length); }
Defining and Adding Values to an Array
In scenarios where we know the size and the values that we want to add to an array, we can initialize our array differently:
var students = new string[2] {"John", "Doe"};
We can see that not only do we specify the length but also add two string
values directly to the students
array during the initialization process.
Let’s validate that the length of the students
array is two:
[TestMethod] public void GivenLengthAndValues_ThenReturnCorrectLength() { var students = new string[2] {"John", "Doe"}; Assert.AreEqual(2, students.Length); }
Defining an Array With Elements
Let’s assume we have a list of student names that we intend to store in an array. However, we don’t know how many names we have to store. We can initialize the same array with values without specifying the length of the array:
var students = new string[] {"John", "Doe"};
We can also achieve the same result using different techniques:
string[] teachers = {"Peter", "John"}; var parents = new[] {"Mary", "Martha"};
Now, we can write some tests to verify that each array has a length of two:
[TestMethod] public void GivenArrayValues_ThenCreateArrayOfFixedLength() { var students = new string[] {"John", "Doe"}; string[] teachers = {"Peter", "John"}; var parents = new[] {"Mary", "Martha"}; Assert.AreEqual(2, students.Length); Assert.AreEqual(2, parents.Length); Assert.AreEqual(2, teachers.Length); }
With our example, we can prove that we can initialize an array directly with values. The compiler infers the array’s length during runtime. Therefore, we do not need to specify the number of elements while initializing it.
Initializing Arrays Through Extensions
Assuming we have data from complex data structures that we need to convert into arrays, we can use the inbuilt ToArray()
extension method to convert an object into an array. Let’s assume we have a List<string>
object that we need to convert into an array:
var studentList = new List<string> {"John", "Doe"};
We can use the ToArray()
extension to convert the studentList
object into an array and verify that it has two values:
[TestMethod] public void GivenListValues_ThenConvertToArray() { var studentList = new List<string> {"John", "Doe"}; var students = studentList.ToArray(); Assert.AreEqual(2, students.Length); }
Should we Use List or Array
In situations where we are not sure of the number of elements and the values that we are going to store in an array, collections such as lists are the best options. These collections are memory-efficient as they can scale as the number of elements changes and we can convert them into arrays.
Besides that, in scenarios where we know the number of elements or intend to initialize an array with its values, we can use the techniques we have discussed above.
Conclusion
In this article, we have learned how to initialize arrays in C# using different techniques. Additionally, we’ve shown several tests to prove the size of our initialized arrays.