Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic vs object type

I have used the dynamic and the object type interchangeably. Is there any difference between these two types? Is there any performance implications of using one over the other? Which one of these is more flexible?

like image 447
Luke101 Avatar asked Aug 09 '10 18:08

Luke101


People also ask

What is the difference between dynamic and object?

Object is useful when we don't have more information about the data type. Dynamic is useful when we need to code using reflection or dynamic languages or with the COM objects and when getting result out of the LinQ queries.

What is the dynamic type of an object?

Dynamic objects expose members such as properties and methods at run time, instead of at compile time. This enables you to create objects to work with structures that do not match a static type or format.

What is dynamic data type?

Dynamic data types are dynamic in nature and don't require initialization at the time of declaration. It also means that a dynamic type does not have a predefined type and can be used to store any type of data. We can define this data type using the keyword “dynamic" in our code.

What is the dynamic type of an object in Java?

Dynamic typing is when a language requires the variable to be initialized. eg. So, because Java requires a local variable to be definitely assigned before it is used, it is not statically typed? I think you mean to say that a static type system requires a type to be specified for each variable in its declaration.


2 Answers

They're hugely different.

If you use dynamic you're opting into dynamic typing, and thus opting out of compile-time checking for the most part. And yes, it's less performant than using static typing where you can use static typing.

However, you can't do much with the object type anyway - it has hardly any members. Where do you find yourself using it? When you want to write general purpose code which can work with a variety of types, you should usually consider generics rather than object.

like image 86
Jon Skeet Avatar answered Oct 20 '22 00:10

Jon Skeet


With the advancement in C# language, we have seen the dynamic and object types. Here are the two types, as I learned by comparing these 7 points:

Object

  1. Microsoft introduced the Object type in C# 1.0.
  2. It can store any value because "object" is the base class of all types in the .NET framework.
  3. Compiler has little information about the type.
  4. We can pass the object type as a method argument, and the method also can return the object type.
  5. We need to cast object variables to the original type to use it and to perform desired operations.
  6. Object can cause problems at run time if the stored value is not converted or cast to the underlying data type.
  7. Useful when we don't have more information about the data type.

Dynamic

  1. Dynamic was introduced with C# 4.0
  2. It can store any type of variable, similar to how Visual Basic handles a variable.
  3. It is not type-safe, i.e., the compiler doesn't have any information about the type of variable.
  4. A method can both accept a Dynamic type as an argument and return it.
  5. Casting is not required, but you need to know the properties and methods related to stored type.
  6. The Dynamic type can cause problems if the wrong properties or methods are accessed because all the information about the stored value is resolved at run time, compared to at compilation.
  7. Useful when we need to code using reflection or dynamic languages or with the COM objects due to writing less code.

Hopefully, this would help somebody.

like image 33
Vijay Bansal Avatar answered Oct 19 '22 23:10

Vijay Bansal