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
Errors at run-time
Error 1
If written code not get validated at run-time then at run time it throw error.
Early Binding : Compile time checking
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()
{
}
}
Error 1
Error 2





No comments:
Post a Comment