C# Dynamic Type

In c# 4.0, the new dynamic type has been introduced to bypass the type checking at compile-time instead, it will resolve the type at runtime. In c#, the dynamic type is same as implicitly typed local variable var but the only difference is the type checking will happen at runtime.

 

The dynamic type variables can be defined by using a dynamic keyword like as shown below.

 

dynamic x = 50;

dynamic y = 10.25;

In the above declarations, we used a dynamic keyword to define the dynamic variables. In most cases, the compiler will compile the variables of dynamic type into the variables of object type but those are not resolved by the compiler during compile-time instead, those will be evaluated at run time.

 

During compile time, the above dynamic declarations will be compiled into object types like as shown below.

 

object x = 50;

object y = 10.25;

As discussed, the actual type will be assigned to the dynamic variables at runtime based on the right side value of the initialization statement.

C# Dynamic Type Example

Following 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 TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            dynamic i = 100;

            Console.WriteLine("i value:{0}, type: {1}", i, i.GetType());

            dynamic j = "Welcome to Tutlane";

            Console.WriteLine("j value:{0}, type: {1}", j, j.GetType());

            dynamic k = true;

            Console.WriteLine("k value:{0}, type: {1}", k, k.GetType());

            dynamic l = 20.50;

            Console.WriteLine("l value:{0}, type: {1}", l, l.GetType());

            Console.ReadLine();

        }

    }

}

If you observe the above example, we declared and initialized the dynamic variables with different values and the actual type will be assigned to the dynamic variables at runtime based on the right side value of initialization statement.

 

When we execute the above example, we will get the result like as shown below.

 

i value:100, type: System.Int32

j value:Welcome to Tutlane, type: System.String

k value:True, type: System.Boolean

l value:20.5, type: System.Double

C# Dynamic Type as Method Parameter

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

 

Following is the example of using dynamic type as a method parameter in c#.

 

using System;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            GetDetails(100);

            GetDetails("Welcome to Tutlane");

            GetDetails(true);

            GetDetails(20.50);

            Console.ReadLine();

        }

        static void GetDetails(dynamic d)

        {

            Console.WriteLine(d);

        }

    }

}

If you observe the above example, we defined GetDetails method with a dynamic parameter so it will accept any type of parameter that’s the reason we are calling GetDetails() method by sending a different type of values.

 

When we execute the above example, we will get the result like as shown below.

 

100

Welcome to Tutlane

True

20.5

C# Assign Class Object to Dynamic Type

In c#, if we assign any class object to dynamic type, then that class object properties and methods will expose only at runtime so the compiler will not throw any exception even if we try to access the methods or properties which do not exist in the class.

 

Following is the example of assigning a class object to dynamic type and accessing properties and methods in c#.

 

using System;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            dynamic dobj = new User();

            dobj.GetInfo();

        }

    }

    class User

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public void GetDetails(dynamic d)

        {

            Console.WriteLine(d);

        }

    }

}

If you observe the above example, we created a dynamic variable “dobj” and assigned User class object and trying to access GetInfo() method which is not existing in User class. However, the compiler will not throw an exception because it will skip the type checking for dynamic type during compile time but we will get runtime exception like as shown below.

 

C# Assign Class Object to Dynamic Type Example Result

 

C# Dynamic Type Overview

Following are the important points which we need to remember about dynamic type in c#.

 

  • The dynamic type 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 variables of dynamic type into the variables of the object type.
  • If we 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.
 

PREVIOUS

C# Var Keyword
 
 

Topics Covered