What are Dynamic Arrays in C#?

C# Dynamic array

C# supports both static and dynamic arrays. If you're new to arrays, check out Arrays in C# (Download Sample Project).

A static array has a fixed size and is defined when an array is declared. The following code defines an array that can hold 5 int-type data only.

int[] odds = new int[5];

Arrays in C# are the 0th index. That means the first item of an array starts at the 0th position. The position of the last item on an array will be a total number of items - 1. So, if an array has five items, the last item of the array is accessed using index 4.

The following code adds five items to the array.

odds[0] = 1;  
odds[1] = 3;  
odds[2] = 5;  
odds[3] = 7;  
odds[4] = 9;

In a fixed array, if you try to add more items than its range, you will get the following error.

odds[5] = 11;

Unhandled exception. System.IndexOutOfRangeException: The index was outside the bounds of the array.
at Program.Main(String[] args) in C:\Mahesh\DotNetCore\ArraysFAQ\ArraysFAQ\Program.cs:line 14

A dynamic array does not have a predefined size. The size of an active array increases as you add new items to the array. You can declare an array of fixed length or dynamic. You can even change a dynamic array to static after it is defined. The following code snippet declares a dynamic array where the size of the array is not provided.

int[] numArray = new int[] {};

Dynamic arrays can be initialized as static arrays. The following code snippet declares a dynamic array and initializes.

int[] numArray = new int[] { 1, 3, 5, 7, 9, 11, 13 };

The following code sample declares three dynamic arrays of different data types.

// Dynamic array of int type  
int[] numArray = new int[] {1, 3, 5, 7, 9, 11 };  
// Dynamic array of string type  
string[] strArray = new string[] { "Mahesh Chand",  
"David McCarter", "Allen O'neill", "Chris Love" };  
// Dynamic array of a collection  
List<string> authors = new List<string>();  
authors.Add("Mahesh Chand");  
authors.Add("David McCarter");  
authors.Add("Allen O'neill");  
authors.Add("Chris Love");

Access C# dynamic arrays

We can access both dynamic arrays and static arrays the same way by passing the item index in the array.

// Dynamic array of int type  
int[] numArray = new int[] {1, 3, 5, 7, 9, 11 };  
Console.WriteLine(numArray[0]);  
Console.WriteLine(numArray[1]);  
Console.WriteLine(numArray[2]);  
Console.WriteLine(numArray[3]);  
Console.WriteLine(numArray[4]);  
Console.WriteLine(numArray[5]);

A foreach loop can be used to iterate through the elements of an array.

// Dynamic array of string type  
string[] strArray = new string[] { "Mahesh Chand",  
"David McCarter", "Allen O'neill", "Chris Love" };  
foreach (string str in strArray)  
Console.WriteLine(str);

When to use dynamic arrays

Unless you limit an array size, the default arrays are dynamic arrays. Dynamic arrays are used when you’re unsure about the number of elements an array can store. Static arrays should be used when you do not want an array to hold more items than it is predefined size.

Summary

In this post, we learned about C# dynamic arrays. Next detailed tutorial, Arrays in C# (Download Sample Project).


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.