Dynamic type Vs Object type
Each object in C# is derived from object type , either directly or indirectly. It is compile time variable and require boxing and unboxing for conversion and it makes it slow. You can change value type to reference type and vice versa.
Object type variables require to cast object variable to original type before using it. It means values of any types can be stored in object type variable. But type conversion (un-boxing) is required to get original type when the value of variable is retrieved in order to use it. It's actually increasing the overhead when we do this both operation. Allows to perform operation of given type once it gets cast any user defined or primitive data type . Also, Object type is useful when it doesn't have more information about the data type.
Dynamic is run time variable and not require boxing and unboxing . You can assign and value to dynamic and also can change value type stored in same. All errors on dynamic can be discovered at run time only. We an also say that dynamic is a run time object which can hold any type of data.
For Dynamic type Casting is not required but you need to know the property and methods related to stored type. Dynamic type is useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code.
- What are the Default Access Modifiers in C#?
- What is a sealed class in C#?
- What is operator overloading in c#?
- Difference Between Finalize and Dispose Method in C#?
- What is an Object Pool in .Net?
- What is Anonymous type in C#?
- What is the difference between Array and ArrayList in C#?
- Difference Between Clone() and CopyTo() Array
- Difference between throw and throw new Exception() in C# ?
- What is the use of finally block in C#?