Static Binding in C#
That
is, unlike JavaScript, or, PHP, we had to specify the type of the variables at
compile time. As long as compiler was unhappy with the variable type
declarations, we had to make correction of the types in order to be able to
compile and run the program.
Some example of static binding
int a = 5;
string s = "Hello world";
int c# 3.0 a new arrived keyword var was thought to be a dynamic binding
var a = 5;
var s = "Hello world";
but it was a mith as var is just another form of static binding, which infers its actual type at complie time.
so at compile time it actually becomes
int a= 5; // for var a= 5;
and
string s = "Hello world"; // for var s = "Hello world";
And what u cant do with var.
var a;
a= 5; // This is now allowed
In var you will be able to know in the intelisese of visual studio the correct type of the object which shows you that is binded at complie time and not at runtime.
Dynamic Binding in C#
In c# 4.0 a new keyword as introduced "dynamic" which allows to bind variables declared to be bound with there types at runtime.
dynamic a = 5;
The complier does not know the type of the variable till the time the statement is executed as in intelisense also it does not show you the correct type of it.
so in dynamic it is leagl to declare vairable as below.
dynamic a;
a= 5;
No comments:
Post a Comment