Dynamic Type Array in C# 4.0

Background
 
You have often seen that an array can be stored with similar data types of values but we can store various data types of values in a single array using the C# 4.0 dynamic keyword.
 
Let us learn the definition of array so beginners can also understand.
 
What an Array is
 
An array is a collection of the same data type values under a single variable.
 
The preceding definition assumes that the array can store only those types of values that the array variable is declared as. Suppose an array is declared using the int data type then only integer type values are stored, but you can use the dynamic keyword to store any type of value in an array.
If you declare an array using the dynamic keyword then the array can be defined as a  collection of various data type values under the single variable.
Now before proceeding towards declaring a dynamic array let us briefly define what the dynamic keyword does.
 
What the dynamic keyword is

Dynamic is the keyword ntroduced in the C# 4.0 version that defines the data type of a variable at run time.
Let us see the following console application that demonstrates a dynamic type array. Create a console application as in the following:
  1. Open Visual Studio from Start - - All programs -- Microsoft Visual Studio.
  2. Then go to to "File" -> "New" -> "Project..." then select Visual C# -> Windows -> Console application.
  3. Then specify the name such as DynamicArray or whatever name you wish and the location of the project and click on the OK button. The new project is created.

Open the Program.cs file and declare an array as:

  1. dynamic Emp=new dynamic[6]; 
In the preceding declaration, we have declared the array using the dynamic keyword that stores 6 elements starting from Index 0 to the ending 5.
Now store the following data type values in the Emp array as:
  1.            dynamic Emp=new dynamic[6];  
  2.            Emp[0] ="Vithal Wajde"//string  
  3.            Emp[1] = 12.50;         //float  
  4.            Emp[2] = 1;             //integer  
  5.            Emp[3] = 'V';           //charcter  
  6.            Emp[4] = 0.65F;         //Double  
  7.            Emp[5] =DateTime.Now;   //Date 
 In the preceding example we have assigned the various data type values for the array index as you can see in the above, now the entire code in the program.cs file of the console application will look such as follows:
  1. using System;  
  2.   
  3. namespace DynamicArray  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             dynamic Emp=new dynamic[6];  
  10.             Emp[0] ="Vithal Wajde"//string  
  11.             Emp[1] = 12.50;         //float  
  12.             Emp[2] = 1;             //integer  
  13.             Emp[3] = 'V';           //charcter  
  14.             Emp[4] = 0.65F;         //Double  
  15.             Emp[5] =DateTime.Now;   //Date  
  16.             Console.WriteLine("Dynamic Type Array Output");  
  17.             foreach (var Rec in Emp)  
  18.             {  
  19.                 Console.WriteLine(Environment.NewLine+Rec);               
  20.             }              
  21.             Console.ReadLine();   
  22.         }  
  23.     }  

 Now run the application, the output will be as follows:
 
In the preceding example its clear that we can also store the various data type values in an array using the dynamic keyword.

Notes
  • Download the Zip file from the attachment for the full source code of the application.
  • We can also store various data type values in an array variable using the object keyword.
Summary

In the preceding article, I have briefly explained dynamic arrays to make them understandable to beginners and newcomers. If you have any suggestion regarding this article then please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all