find and solve || findandsolve.com
Please wait.....

Dynamic Keyword in C#

The C# 4 introduces a new type, dynamic keyword. The type is static, but an object of type dynamic bypasses static type checking.

In C# 4.0, a new type is introduced, known as a dynamic type. It is used to avoid compile-time type checking. The compiler does not check the type of the dynamic type variable at compile-time, instead of this, the compiler gets the type at the run time. The dynamic type variable is created using the dynamic keyword.


We can define dynamic type variables using a dynamic keyword like as given below.

dynamic a = 30;
dynamic b = 15.25;

 In the given above declarations, you can use a dynamic keyword to define the dynamic variables. The compiler will compile the variables of dynamic type into the object type variables in most cases, but the compiler does not resolve those during compile-time; instead, those will evaluate at run time.

object a = 30;

object b= 15.25;


C# Dynamic Type Example


The given example will show how to declare dynamic variables and how a dynamic variable will change its type based on the right-side value of its initialization statement.

using System;

namespace FindAndSolve
{
    public class Program
    {
       public static void Main(string[] args)
       {
          dynamic a = 80;
          Console.WriteLine("a value:{0}, type: {1}", a, a.GetType());
          dynamic b = "Welcome to FindAndSolve";
          Console.WriteLine("b value:{0}, type: {1}", b, b.GetType());
          dynamic c = true;
          Console.WriteLine("c value:{0}, type: {1}", c, c.GetType());
          dynamic d = 15.25;
          Console.WriteLine("d value:{0}, type: {1}", d, d.GetType());
          Console.ReadLine();
       }
    }
}


Output

a value:80, type: System.Int32

b value:Welcome to FindAndSolve, type: System.String

c value:True, type: System.Boolean

d value:15.25, type: System.Double


C# Dynamic Type as Method Parameter


If you use dynamic type as a method parameter, then it will accept any type of parameter at run time.

In the given below using dynamic type as a method parameter in c#.

using System;

namespace FindAndSolve
{
  public class Program
    {
      public void Main(string[]args)
       {
          GetDetails(80);
          GetDetails("Welcome to FindAndSolve");
          GetDetails(true);
          GetDetails(15.25);
          Console.ReadLine();
       }
       public static void GetDetails(dynamic dym)
       {
          Console.WriteLine(dym);
       }
    }
}

Output

80

Welcome to FindAndSolve

True

15.25


C# Assign Class Object to Dynamic Type


In the c# programming, if you assign any class object to a dynamic type, the class object properties and methods will be exposed only at runtime. So, the compiler will not throw an exception even if you try to access the properties or methods which do not exist in the class.

In the given example of assigning the class object to dynamic type and accessing properties and methods in c# programming language.

using System;

namespace FindAndSolve
{
    public class Program
    {
       public static void Main(string[] args)
       {
          dynamic objDynamic= new UserInfo();
          objDynamic.GetInfo();
       }
    }
    public class UserInfo
    {
       public int Id { get; set; }
       public string Name { get; set; }
       public void GetDetails(dynamic dym)
       {
          Console.WriteLine(dym);
       }
    }
}


Output

Run-time exception (line 10): 'FindAndSolve.UserInfo' does not contain a definition for 'GetInfo'

Stack Trace:

[Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'FindAndSolve.UserInfo' does not contain a definition for 'GetInfo']

   at CallSite.Target(Closure , CallSite , Object )

   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1[T0](CallSite site, T0 arg0)

   at FindAndSolve.Program.Main(String[] args) :line 10


If you observe the given above example, we created a dynamic variable “objDynamic” and assigned the UserInfo class object. We tried to access the GetInfo() method that does not exist in the UserInfo class.


C# Dynamic Type Overview


  • In the c# dynamic type keyword is useful to bypass the type checking at compile-time. Instead, it will resolve the type at runtime.
  • During compile-time, the compiler will compile the dynamic type variables into the object type variables.
  • If you use dynamic type as a method parameter, then it will accept any type of parameter at run time.
  • Visual studio will not provide any IntelliSense support for dynamic types.