Dynamic Variable In C# Language

Introduction

Dynamic is a new keyword , which is include in c# 4.0 version in 2010. Dynamic keyword manage by the Dynamic Language Runtime (DLR), which work as a Common Language Runtime (CLR). Dynamic variable not  define as type, because dynamic variable manage by the Dynamic Language Runtime (DLR).    Dynamic variable is declared as having type dynamic, operations on these value are not done or verified at compile time, but instead happen entirely at runtime. 

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace dynamic_variable_in_c_sharp

{

    class Program

    {

        static void Main(string[] args)

        {

            dynamic a = 10;        //Create Dynamic variable a, which value assige as int type

            dynamic b = "Hello";   //Create Dynamic variable b, which value assige as string type

            dynamic c = 5.4;       //Create Dynamic variable c, which value assige as double type

            dynamic result1;      

            dynamic result2;

            dynamic result3;

            result1 = a + b;      //Dynamic variable result1, which store sum of int and string type value

            result2 = b + c;     //Dynamic variable result2, which store sum of string and double type value

            result3 = c + a;     //Dynamic variable result3, which store sum of double and int type value

 

            Console.WriteLine("Sum of Dynamic value a + b is  : {0}",result1);

            Console.WriteLine("Sum of Dynamic value b + c is  : {0}", result2);

            Console.WriteLine("Sum of Dynamic value c + a is  : {0}", result3);

            Console.ReadLine();

 

        }

    }

}


Output:


output.jpg


Ebook Download
View all
Learn
View all