TechieClues TechieClues
Updated date Mar 01, 2022
In this blog, we will see how to create dynamic arrays in C#. An Аrrаy is а соlleсtiоn оf vаlues оf а similаr dаtа tyрe. С# аrrаys аre а referenсe tyрe. Eасh аrrаy in С# is аn оbjeсt аnd is inherited frоm the System.Аrrаy сlаss.
  • 7.6k
  • 0
  • 0

An Аrrаy is а соlleсtiоn оf vаlues оf а similаr dаtа tyрe. С# аrrаys аre а referenсe tyрe. Eасh аrrаy in С# is аn оbjeсt аnd is inherited frоm the System.Аrrаy сlаss.

Types of Arrays:

  1. Single Dimensional Array
  2. Multidimensional Array
  3. Jagged Array

You can check the types of arrays and examples here, Arrays in C# with Examples.

C# always supports static and dynamic arrays. Static array allocates a sequential memory block and is static (fixed-length) in nature. The below code shows, cars array holds 4 string type data only, you can't add more than that in an array.

// Declaration
string[] cars = new string[4];

// Cars array only hold 4 string data. The index starts with 0.
cars[0] = "Audi";
cars[1] = "BMW";
cars[2] = "Toyota";
cars[3] = "Ford";

As you know, the index starts with the 0th position, so we have added till the 3rd position and the total array count is 4. You are not able to add more than 4. If you try to add the 5th one then you will get the below runtime exception,

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

Declaring C# dynamic arrays

To overcome this problem, we will use dynamic arrays that don't have a predefined length or size. When you add new items to the array, the size will be increased automatically to hold the new items. The below code shows how to create a dynamic array and initializes it without predefined size,

string[] cars = new string[] { "Audi", "BMW" , "Toyota" , "Ford" , "KIA"};

Accessing C# dynamic arrays

Passing the Item Index:

The below code shows how to access the dynamic arrays, Moreover,  You can access both dynamic arrays or static arrays same way by passing the item index in the array. 

static void Main(string[] args)
{
    // Declaration
	string[] cars = new string[] { "Audi", "BMW" , "Toyota" , "Ford" , "KIA"};
    // Print array item/string value
	Console.WriteLine(cars[0]);
	Console.WriteLine(cars[1]);
	Console.WriteLine(cars[2]);
	Console.WriteLine(cars[3]);
	Console.WriteLine(cars[4]);
	Console.ReadKey();
}

Output:

Audi
BMW
Toyota
Ford
KIA

Using Foreach Loop:

You can also use the foreach loop to access the array items. foreach is used to iterate through the elements of an array.

static void Main(string[] args)
{
    // Declaration
	string[] cars = new string[] { "Audi", "BMW" , "Toyota" , "Ford" , "KIA"};
    // Iterate and print array item
	foreach (string car in cars)
	{
		Console.WriteLine(car);
	}                
	Console.ReadKey();
}

Output:

Audi
BMW
Toyota
Ford
KIA

If you want to learn more about Arrays, please visit Working with Arrays in C#

 

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!