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 the array so beginners can also understand.

What is an Array?

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 a single variable.

Now before proceeding toward declaring a dynamic array, let us briefly define what the dynamic keyword does.

What is the dynamic keyword?

Dynamic is the keyword introduced 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 "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.

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.

dynamic[] Emp = new dynamic[6];
Emp[0] = "Vithal Wajde"; // string
Emp[1] = 12.50; // float
Emp[2] = 1; // integer
Emp[3] = 'V'; // character
Emp[4] = 0.65F; // double
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, the entire code in the program.cs file of the console application will look as follows.

using System;
namespace DynamicArray
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic[] Emp = new dynamic[6];
            Emp[0] = "Vithal Wajde";  // string
            Emp[1] = 12.50;          // float
            Emp[2] = 1;              // integer
            Emp[3] = 'V';            // character
            Emp[4] = 0.65F;          // Double
            Emp[5] = DateTime.Now;   // Date
            Console.WriteLine("Dynamic Type Array Output");
            foreach (var Rec in Emp)
            {
                Console.WriteLine(Environment.NewLine + Rec);
            }
            Console.ReadLine();
        }
    }
}

Now run the application the output will be as follows.

output

In the preceding example, it's clear that we can also store the various data type values in an array using the dynamic keyword.

Note

  • 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.


Recommended Free Ebook
Similar Articles