What are Dynamic Arrays in C#

A dynamic array in C# does not have a predefined fixed size. Here is a code example of dynamic arrays in C#.

C# dynamic array
 
C# supports both static and dynamic arrays. If you're new to arrays, check out Working With Arrays in C#.
 
A static array has a fixed size and defined when an array is declared. The following code defines an array that can hold 5 int type data only.
  1. int[] odds = new int[5]; 
Arrays in C# are 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 total number of items - 1. So, if an array has 5 items, the last item of the array is access by using index 4.
 
The following code adds 5 items to the array. 
  1. odds[0] = 1;  
  2. odds[1] = 3;  
  3. odds[2] = 5;  
  4. odds[3] = 7;  
  5. odds[4] = 9;  
 
In a fixed array, if you try to add more items than its range, you will get the following error: 
  1. odds[5] = 11;  
Unhandled exception. System.IndexOutOfRangeException: 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 a dynamic 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. 
  1. int[] numArray = new int[] {};  
 
Dynamic arrays can be initialized as static arrays. The following code snippet declares a dynamic array and initializes. 
  1. int[] numArray = new int[] { 1, 3, 5, 7, 9, 11, 13 }; 
 
The following code sample declares 3 dynamic arrays of different data types. 
  1. // Dynamic array of int type  
  2. int[] numArray = new int[] {1, 3, 5, 7, 9, 11 };  
  3. // Dynamic array of string type  
  4. string[] strArray = new string[] { "Mahesh Chand",  
  5. "David McCarter""Allen O'neill""Chris Love" };  
  6. // Dynamic array of a collection  
  7. List<string> authors = new List<string>();  
  8. authors.Add("Mahesh Chand");  
  9. authors.Add("David McCarter");  
  10. authors.Add("Allen O'neill");  
  11. authors.Add("Chris Love"); 
 
Accessing C# dynamic arrays
 
We can access both dynamic arrays or static arrays same way by passing the item index in the array. 
  1. // Dynamic array of int type  
  2. int[] numArray = new int[] {1, 3, 5, 7, 9, 11 };  
  3. Console.WriteLine(numArray[0]);  
  4. Console.WriteLine(numArray[1]);  
  5. Console.WriteLine(numArray[2]);  
  6. Console.WriteLine(numArray[3]);  
  7. Console.WriteLine(numArray[4]);  
  8. Console.WriteLine(numArray[5]);  
A foreach loop can be used to iterate through the elements of an array. 
  1. // Dynamic array of string type  
  2. string[] strArray = new string[] { "Mahesh Chand",  
  3. "David McCarter""Allen O'neill""Chris Love" };  
  4. foreach (string str in strArray)  
  5. 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 not sure 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 that its predefined size.
 
ArrayList collection
 
The ArrayList class is often used to work with a collection of objects. It is a dynamic collection and provides built-in methods to work with list items such as add items, remove items, copy, clone, search, and sort array.
 
Learn more about ArrayList, visit ArrayList in C#.
 
Summary
 
In this post, we learned about C# dynamic arrays. Next detailed tutorial, Working with Arrays in C# https://www.c-sharpcorner.com/article/working-with-arrays-in-C-Sharp/