Thursday, November 12, 2015

Dynamic Keyword

dynamic is a type introduced in C# 2010, making a variable as dynamic type, skip compile time checking for that variable, it allows any operation / call to write at compile time.
If written code not get validated at run-time then at run time it throw error.
dynamic keyword is an example of Late Binding
Early Binding : Compile time checking


Late Binding : Run time checking not compile time

dynamic converts early binding to late binding

Source Code: 
Below code compiled successfully
    class Program
    {
        static void Main(string[] args)
        {
            //Example1 allowing mathematical operation on string at compile time
            dynamic str = "hello";
            str++;

            //Example2 allowing Update() method call that does not exist, at compile time
            dynamic objStudent = new student();
            objStudent.Update();
        }
    }
    class student
    {
        public int RollNo { get; set; }
        public string Name { get; set; }
        public void Add()
        {
        }
    }


Errors at run-time
Error 1

Error 2


No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...