Search code examples
dynamicboxingunboxing

Programming the "dynamic" type in C#


When we use dynamic type over object type

weather we can overcome Boxing/UnBoxing overhead ?

void Print(dynamic p)
{
     Console.WriteLine(string.Format("{0} : {1}", p.GetType(),p));
}

void Print(object p)
{
     Console.WriteLine(string.Format("{0} : {1}", p.GetType(),p));
}

From both the method which one will be efficient and friendly to processor ?


Solution

  • There is likely no difference at all. From Microsoft:

    Type dynamic behaves like type object in most circumstances. However, operations that contain expressions of type dynamic are not resolved or type checked by the compiler.

    http://msdn.microsoft.com/en-us/library/vstudio/dd264741.aspx

    From this I infer that dynamic will act as an object until it is used in an expression. This includes parameter passing.