.Net C# dynamic typing

C# 4 introduced dynamic typing into the language specification, this give the developer the ability to declare something that can be decided at run-time rather than compile-time. One of the advantages of this feature is the ability to deal with uncertain types such as objects returned from COM. It also allows C# to emulate the behaviour of languages such as: Perl, Ruby, Python etc.

using System;

namespace CsharpDynamicDemo
{
    class CsharpDynamicExample
    {
        static void Main()
        {
            dynamic dynamicVariable = "Hello I'm dynamic.";

            const string staticVariable = "I'm static.";

            Console.WriteLine(dynamicVariable.GetType());
            
            Console.WriteLine(staticVariable.GetType());

            Console.ReadKey();
        }
    }
}

Returns:

System.String
System.String

Leave a Reply

Your email address will not be published. Required fields are marked *