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?
-
C# dynamic adds functionality that has been missing in C#. VB.net's Object (with option strict off) is the same as dynamic. https://stackoverflow.com/questions/2889974/vb-net-equivalent-for-c-sharp-dynamic-with-option-strict-on – Brain2000 Nov 02 '19 at 04:20
5 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
.

- 1,421,763
- 867
- 9,128
- 9,194
-
If Luke just called the handful of methods available in `System.Object`, would they also be called dynamically? – Steven Sudit Aug 09 '10 at 18:18
-
2@Steven: Yes, it does - even for `GetType` which is non-virtual! (You can set a delegate to react to `GetType` calls on `ExpandoObject`, for example. Scary stuff.) – Jon Skeet Aug 09 '10 at 18:29
-
That's what I was afraid of. Looks like we'd need to "box" a `dynamic` by upcasting to `object` if we even want to know the truth about its type. :-) – Steven Sudit Aug 09 '10 at 18:31
-
@Steven: That wouldn't perform any *actual* boxing. It might involve a dynamic attempt at conversion, but the underlying field (or local variable) type for a dynamic variable is already `object`. – Jon Skeet Aug 09 '10 at 18:43
-
Yes, that's why I put "box" in scare-quotes. I meant assigning the `dynamic` instance to an `object` alias to avoid the run-time check when calling `GetType`. But now that you mention it, there's no reason to think that the cast will be performed statically. Perhaps it could try to define an implicit conversion! – Steven Sudit Aug 09 '10 at 18:49
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
- Microsoft introduced the Object type in C# 1.0.
- It can store any value because "object" is the base class of all types in the .NET framework.
- Compiler has little information about the type.
- We can pass the object type as a method argument, and the method also can return the object type.
- We need to cast object variables to the original type to use it and to perform desired operations.
- Object can cause problems at run time if the stored value is not converted or cast to the underlying data type.
- Useful when we don't have more information about the data type.
Dynamic
- Dynamic was introduced with C# 4.0
- It can store any type of variable, similar to how Visual Basic handles a variable.
- It is not type-safe, i.e., the compiler doesn't have any information about the type of variable.
- A method can both accept a Dynamic type as an argument and return it.
- Casting is not required, but you need to know the properties and methods related to stored type.
- 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.
- 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.

- 5,753
- 72
- 57
- 129

- 737
- 7
- 10
-
13It is my understanding that var is not a type at all -- it just implicitly decides the type based on the initialized value. For example: var x = 2; x is NOT a var type, it is an int type. – Nanomurf Sep 26 '17 at 20:05
-
5
In simple language:
Assume we have the following method:
public static void ConsoleWrite(string inputArg)
{
Console.WriteLine(inputArg);
}
Object: the following code has compile error unless cast object to string:
public static void Main(string[] args)
{
object obj = "String Sample";
ConsoleWrite(obj);// compile error
ConsoleWrite((string)obj); // correct
Console.ReadKey();
}
dynamic: the following code compiles successfully but if it contains a value except string
it throws Runtime error
public static void Main(string[] args)
{
dynamic dyn = "String Sample";
ConsoleWrite(dyn); // correct
dyn = 1;
ConsoleWrite(dyn);// Runtime Error
Console.ReadKey();
}

- 21,578
- 41
- 164
- 232
-
2For dynamic also it will not throw error as you mentioned in your last sample. it will compile and return same result as it would for object type. check here: https://dotnetfiddle.net/l5K4Cl – MVijayvargia Apr 10 '22 at 16:28
-
The code provided in the answer will throw an error, but the code you added in the https://dotnetfiddle.net/l5K4Cl will work as you said. It's a bit different code. – Bartłomiej Mucha Dec 30 '22 at 15:25
There is an article explaining the different types, including object and dynamic types. The article also explains the difference between the two with a nice example.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/reference-types
I shall give a short gist of the difference explained in the article:
The object type is an alias for System.Object in .NET. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from System.Object. You can assign values of any type to variables of type object.
The dynamic type indicates that use of the variable and references to its members bypass compile-time type checking. Instead, these operations are resolved at run time. The dynamic type simplifies access to COM APIs such as the Office Automation APIs, to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).
Type dynamic behaves like type object in most circumstances. In particular, any non-null expression can be converted to the dynamic type. The dynamic type differs from object in that operations that contain expressions of type dynamic are not resolved or type checked by the compiler. The compiler packages together information about the operation, and that information is later used to evaluate the operation at run time. As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time.
Summary:
What this essentially means is object is a type and all other types inherit from it. Dynamic is not really a type, it is more like a pointer or representation of some other type, which will be resolved at run time.

- 1,455
- 1
- 13
- 15
Dynamic: Casting is not required but you need to know the property and methods related to stored type to avoid error in run time.
dynamic dyn = 1;
Console.WriteLine(dyn);
int a = dyn;// works fine
Console.WriteLine(a);//print 1
Console.ReadKey();
The above code will work just fine but if dyn = 1.2 is supplied then it will throw exception as 1.2 cannot converted to int
Object: Require to cast object variable explicitly.
object ob = 1;//or 1.2
Console.WriteLine(ob);
int a = ob;//Compile error because explicit casting is not done
Console.WriteLine(a);
Console.ReadKey();
Fiddle: https://dotnetfiddle.net/l5K4Cl

- 331
- 2
- 10