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:
- Open Visual Studio from Start - - All programs -- Microsoft Visual Studio.
- Then go to to "File" -> "New" -> "Project..." then select Visual C# -> Windows -> Console application.
- 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 as:
- dynamic Emp=new dynamic[6];
- Emp[0] ="Vithal Wajde";
- Emp[1] = 12.50;
- Emp[2] = 1;
- Emp[3] = 'V';
- Emp[4] = 0.65F;
- Emp[5] =DateTime.Now;
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:
- using System;
-
- namespace DynamicArray
- {
- class Program
- {
- static void Main(string[] args)
- {
- dynamic Emp=new dynamic[6];
- Emp[0] ="Vithal Wajde";
- Emp[1] = 12.50;
- Emp[2] = 1;
- Emp[3] = 'V';
- Emp[4] = 0.65F;
- Emp[5] =DateTime.Now;
- 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:
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.