Dynamic Typing in C#

Well, C# provides this option to prove that it is flexible. If you are too fond of dynamic typing, use scripting languages such as PHP!

Below is an example of how to use it – you will see that program will compile without errors and will display two different values for a as intended – showing that you just dynamically typed variable ‘a’ across two different types – integer and string.

namespace DynamicTyping
{
    class Program
    {
        static void Main(string[] args)
        {
 
            dynamic a = 1;
            var i = a + 2;
            a = "Test Test";
 
            Console.WriteLine(i);
            Console.WriteLine(a);
        }
    }
}

Add a Comment

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