Tuesday, June 30, 2009

"dynamic" keyword in C# (.NET Framework 4.0)

"dynamic" keyword acts very similar to "object" type, but the type of "dynamic" variables are resolved at runtime. So, any member of the object accessed at compiled would get through the process but may result in error at run time.

For example,

If the object is defined as follows

class TestObject
{
void Method1(int param1)
{
}
}

//while accessing the object
dynamic dynObject = new TestObject();

//The following call throws error at RUNTIME ONLY
dynObject.Method1(10,20) //Notice two params



No comments:

Post a Comment