Table of contents
1.
Introduction
2.
Object arrays
3.
Dynamic arrays
4.
FAQs
4.1.
What is an array?
4.2.
What is an object array in C#?
4.3.
What is a dynamic array in C#?
4.4.
Is an ArrayList a dynamic array?
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Object and Dynamic Array in C#

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

An Array in C# is a collection of elements with their unique index to access them. It is a sequential collection and only supports variables with similar data types. All the elements in the array get stored in the contiguous memory location. The size of the array is fixed and static. The default value of an element in a numeric array is 0 (Zero). The index of an array always starts from 0 and ends with the index n-1(n is the length of the array). Let’s learn about the object and dynamic arrays in this article.

Object arrays

The collection of different elements with different data types in a single array is called an object array. They are versatile and increase the complexity of a program. The object array contains the references to any derived type instances as elements. Let’s learn how to create an object array and insert values into it.

using System; 
  
class ObjectArrayExample { 
    public static void main() 
    { 

		// Type 1
        object[] arr1 = new object[4]{5, “codingNinjas”, 25.2, ‘E’};
        
        //Type 2
        object[] arr2 = new object[4];

        arr2[0] = 5; 
        arr2[1] = “codingNinjas”; 
        arr2[2] = 25.2; 
        arr2[3] = ‘E’; 
    
        // Print the elements
        Console.WriteLine(“First array:”);
        foreach(var item in arr1) 
        { 
            Console.WriteLine(item); 
        } 
        Console.WriteLine(“Second array:”);
        foreach(var ele in arr2) 
        { 
            Console.WriteLine(ele); 
        }
    } 
}


Output

First array:
5
codingNinjas
25.2
E

Second array:
5
codingNinjas
25.2
E

We can declare and initialize the elements in an array in two ways, as shown in the code above. We can initialize the values while declaring using the braces or assign the values to elements individually later. Both the arrays will give the same result when we print them. 

Must Read IEnumerable vs IQueryable.

Dynamic arrays

A dynamic array is an ordered collection of elements that provides dynamic memory allocation and helps us overcome the disadvantages of a fixed size/static array. The dynamic can be declared without defining any fixed size, so it is growable and allows us to add any number of elements to the array. The size of a dynamic array is defined at the run time after the user gives all the input elements. It allows us to perform operations like inserting, deleting, searching, and sorting elements. Let’s learn how to work with dynamic arrays through the following example.

using System;
using System.Collections;
using System.Collections.Generic;

class dynamicArrayExample {
	public static void main()
	{
		List<int> arr = new List<int>();

		arr.Add(5);
		arr.Add(56);
		arr.Add(29);

		Console.WriteLine("The sorted array is: ");
		arr.Sort();
		foreach(int ele in arr)
		{
			Console.WriteLine(ele);
		}
	}
}


Output

The sorted array is: 

5

29

56

In the above code, we created a dynamic array using the list collection and inserted the elements later. After sorting the array, we get the output as shown above.

You can also read about dynamic array in c.

Related Article: Singleton Design Pattern C#

FAQs

What is an array?

An Array in C# is a collection of elements with their unique index to access them. It is a sequential collection and only supports variables with similar data types. All the elements in the array get stored in the contiguous memory location.

What is an object array in C#?

The collection of different elements with different data types in a single array is called an object array. The object array contains the references to any derived type instances as elements. They are versatile and increase the complexity of a program.

What is a dynamic array in C#?

A dynamic array is an ordered collection of elements that provides dynamic memory allocation. The dynamic can be declared without defining any fixed size, so it is growable and allows us to add any number of elements to the array. 

What is the difference between static array and dynamic array in C#?
The static arrays have a fixed size, and the memory is allocated at the compile time. In contrast, the dynamic array doesn’t have any fixed size, and the memory is allocated dynamically at the runtime from the heap.

Is an ArrayList a dynamic array?

An ArrayList is re-sizable, and the memory is allocated dynamically at run time from a heap similar to a dynamic array. So an ArrayList can also be considered a dynamic array.

Key Takeaways

We have discussed the concept of the object and dynamic arrays in C# in this article with clear examples and a code. You can now use both the object and dynamic arrays in your code.  

Recommended problems -

 

Hey Ninjas! We hope this blog helped you better understand the object and dynamic arrays in C#. Please check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Live masterclass