Object and Dynamic Array 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.

Object Array

Object array is used to store elements of the different types in a single array. In C#, an object reference may point to any derived type instance.

Disadvantages of Object array:



  • It makes code more complex.
  • It decrease the run-time of the program.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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] = "Geeks";
  
        // 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);
        }
    }
}

chevron_right


Output:

3.899
3
g
Geeks

System.Object

Dynamic Array

The dynamic array provides dynamic memory allocation, adding, searching, and sorting elements in the array. Dynamic array overcomes the disadvantage of the static array. In a static array, the size of the array is fixed but in a dynamic array, the size of the array is defined at run-time. List<T> is the dynamic arrays in C#.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// concept of dynamic array
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating and initializing 
        // the value in a dynamic list
        List<int> myarray = new List<int>();
        myarray.Add(23);
        myarray.Add(1);
        myarray.Add(78);
        myarray.Add(45);
        myarray.Add(543);
  
        // Display the element of the list
        Console.WriteLine("Elements are: ");
        foreach(int value in myarray)
        {
            Console.WriteLine(value);
        }
  
        // Sort the elements of the list
        myarray.Sort();
  
        // Display the sorted element of list
        Console.WriteLine("Sorted element");
        foreach(int i in myarray)
        {
            Console.WriteLine(i);
        }
    }
}

chevron_right


Output:

Elements are: 
23
1
78
45
543
Sorted element
1
23
45
78
543


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.