Dynamic Type in C#

In C# 4.0, a new type is introduced that is known as a dynamic type. It is used to avoid the 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 dynamic keyword.

Example:

dynamic value = 123;

Important Points:



  • In most of the cases, the dynamic type behaves like object types.
  • You can get the actual type of the dynamic variable at runtime by using GetType() method. The dynamic type changes its type at the run time based on the value present on the right-hand side. As shown in the below example.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate how to get the
    // actual type of the dynamic type variable
    using System;
      
    class GFG {
      
        // Main Method
        static public void Main()
        {
      
            // Dynamic variables
            dynamic value1 = "GeeksforGeeks";
            dynamic value2 = 123234;
            dynamic value3 = 2132.55;
            dynamic value4 = false;
      
            // Get the actual type of 
            // dynamic variables
            // Using GetType() method
            Console.WriteLine("Get the actual type of value1: {0}",
                                      value1.GetType().ToString());
      
            Console.WriteLine("Get the actual type of value2: {0}",
                                      value2.GetType().ToString());
      
            Console.WriteLine("Get the actual type of value3: {0}",
                                      value3.GetType().ToString());
      
            Console.WriteLine("Get the actual type of value4: {0}"
                                      value4.GetType().ToString());
        }
    }

    chevron_right

    
    

    Output:

    Get the actual type of value1: System.String
    Get the actual type of value2: System.Int32
    Get the actual type of value3: System.Double
    Get the actual type of value4: System.Boolean
    
  • When you assign a class object to the dynamic type, then the compiler does not check for the right method and property name of the dynamic type which holds the custom class object.
  • You can also pass a dynamic type parameter in the method so that the method can accept any type of parameter at run time. As shown in the below example.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate how to pass
    // dynamic type parameters in the method
    using System;
      
    class GFG {
      
        // Method which contains dynamic parameters
        public static void addstr(dynamic s1, dynamic s2)
        {
            Console.WriteLine(s1 + s2);
        }
      
        // Main method
        static public void Main()
        {
      
            // Calling addstr method
            addstr("G", "G");
            addstr("Geeks", "forGeeks");
            addstr("Cat", "Dog");
            addstr("Hello", 1232);
            addstr(12, 30);
        }
    }

    chevron_right

    
    

    Output:

    GG
    GeeksforGeeks
    CatDog
    Hello1232
    42
    
  • The compiler will throw an exception at runtime if the methods and the properties are not compatible.
  • It does not support the intellisense in visual studio.
  • The compiler does not throw an error on dynamic type at compile time if there is no type checking for dynamic type.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.