What are Dynamic Arrays in C#?

 Admin   
191 View    Jan 17 2020 11:04PM

Dynamic arrays are growable arrays and have an advantage over static arrays. This is because the size of an array is fixed.

To create arrays dynamically in C#, use the ArrayList collection. It represents an ordered collection of an object that can be indexed individually. It also allows dynamic memory allocation, adding, searching and sorting items in the list.

The following is an example showing how to create arrays dynamically in C# 

An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location.

// C# program to illustrate the

// concept of object array

using System;

 

class GFG {

 

       // Main method

       static public void Main()

       {

 

                   // Creating and initializing

                   // object array

                   object[] arr = new object[6];

 

                   arr[0] = 3.899;

                   arr[1] = 3;

                   arr[2] = 'g';

                   arr[3] = "Mind";

 

                   // it will display

                   // nothing in output

                   arr[4] = null;

 

                   // it will show System.Object

                   // in output

                   arr[5] = new object();

 

                   // Display the element of

                   // an object array

                   foreach(var item in arr)

                   {

                               Console.WriteLine(item);

                   }

       }

}

 

Output:

3.899

3

g

Geeks

 

System.Object