Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between “dynamic” and “object” keywords in C#? [duplicate]

Tags:

.net

c#-4.0

Can anyone explain in brief about differences between “dynamic” and “object” keywords in C#?

like image 627
Jyoti Prasad Pal Avatar asked Jan 08 '14 12:01

Jyoti Prasad Pal


3 Answers

object :

Let’s take a quick look at the object keyword first. I’m not going to talk a lot about it because it’s been around since C# 1.0. This keyword is nothing more than a shortcut for System.Object, which is the root type in the C# class hierarchy. (However, as Eric Lippert pointed out in his blog post, not everything in C# derives from object.) This is a powerful mechanism, since you can assign almost any value to instances of this type.

Here is a short example that demonstrates some of the benefits and problems of using the object keyword.

object obj = 10;
Console.WriteLine(obj.GetType());
// Prints System.Int32 because 
// this is the type of the value stored in this object.

// A compiler error, because 
// at compile time the type of obj is System.Object.
// obj = obj + 10; 

// You need to explicitly cast obj to a necessary type.
obj = (int)obj + 10;

// However, this does not mean that you are really safe. 
// You can cast to a wrong type 
// and the compiler will not detect it. 
// Here you get an exception at run time, 
// because obj is an integer, not a string.
// obj = (string)obj + 10;

// You also get a run-time exception 
// if you cast to a wrong numeric type, 
// even if there is an implicit conversion in the language.
// obj = (double)obj + 10;

As you can see, although obj stores an integer, the compiler does not let you to perform any mathematical operations without a cast. It may look like casting helps you to ensure that you really have an integer, but it doesn’t. You can cast to a completely different type and the compiler will not detect it. As a result, you get a run-time exception.

So you have to perform an explicit cast that does not guarantee anything just because the compiler doesn’t let you to run your program without a cast.

dynamic :

Here is where the new dynamic keyword in C# 4.0 comes in. It tells the compiler not to enforce additional rules upon your code.

dynamic dyn = 10;
Console.WriteLine(dyn.GetType());
// Same as "object". 
// Prints System.Int32 because 
// this is the type of the value stored in this object.

// No compiler error, because 
// the compiler does not try to identify 
// the type of the dynamic object at compile time.
dyn = dyn + 10;

// Also, this operation will succeed for all numeric 
// or other types that support a “+” operation.
dyn = 10.0;
dyn = dyn + 10;

dyn = "10";
dyn = dyn + 10;

This is one of the main differences between object and dynamic – with dynamic you tell the compiler that the type of an object can be known only at run time, and the compiler doesn’t try to interfere. As a result, you can write less code. And I want to emphasize that this is no more dangerous than using the original object keyword. However, it is not less dangerous either, so all the type-checking techniques that you need to use when operating with objects (such as reflection) have to be used for dynamic objects as well.

The next question that often comes up is something like the following: “Since a dynamic object can be anything and the compiler doesn’t check what it is, does it mean that you can pass a dynamic object to my unsuspecting method/system and make it crash?”

Let’s assume we have a simple method.

public static void Print(string arg)
{
    Console.WriteLine(arg);
}
Now let’s look at how you can pass a dynamic object to it.

dynamic dyn = 10;

// You get an exception at run time here.
Print(dyn);

As you can see, although the compiler allows you to pass a dynamic object to your method, your method never gets this object if it has a wrong type. An exception is thrown before the method is actually called. The only way you can pass a dynamic object to your method is if it contains a necessary value, in this case, a string.

dynamic dyn = "10";
Print(dyn);

Again, this is not that much different from the behavior you get with the object keyword.

object obj = 10;

// Doesn't compile.
//Print(obj);

// Compiles, but there is an exception at run time.
//Print((string)obj);

// This code works because obj is now a string, 
// but you still need a cast.
obj = "10";
Print((string)obj);

Some people say that it isn’t hard to read (int)obj, so why bother with dynamic? Well, there are situations when you have to perform so many cast operations that it makes your code almost unreadable. There are also situations when a simple cast is not enough and you have to call reflection methods, such as InvokeMember or GetProperties. A good example here is COM interop, which is why it was modified to use the new dynamic features (for more information, look at this “how-to”.)

Also, the dynamic keyword and dynamic language runtime enable many scenarios that were either impossible or difficult to do before, including interoperation with dynamic languages. I have highlighted couple of such scenarios earlier on this blog: Introducing ExpandoObject and Creating Wrappers with DynamicObject.

Conclusion :

The conclusion is that there is no need to fear that someone can break your code by using dynamic features. It is no more (and again, no less) dangerous than the object keyword.

So, if you often use the object keyword and have to perform a lot of casting and/or use reflection to call methods and properties of objects, you probably should take a look at the dynamic keyword. In some cases it’s more convenient than object and with less code to write.

like image 175
Arpit Jain Avatar answered Oct 07 '22 08:10

Arpit Jain


Please check this talk by Anders Hejlsberg for getting a better understanding of dynamic.

Also check on this page the C# keywords.

Those keywords refer to .NET common types.

Basically: The object type is an alias for Object in the .NET Framework. In the unified type system of C#, all types, predefined and user-defined, reference types and value types (including dynamic), inherit directly or indirectly from Object. You can assign values of any type to variables of type object.

Talking about dynamic:

The dynamic type enables the operations in which it occurs to 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, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).

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. 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.

Note that this explanation is taken from the MSDN page.

like image 45
avenet Avatar answered Oct 07 '22 07:10

avenet


  • object is exactly that: a plain C# object with only a bare minimum of methods. In .NET, it serves as a built-in base class for everything (making sure that everything has at least ToString(), Equals(), and GetHashCode()).
  • dynamic objects on the contrary are a totally different breed. dynamic allows for late binding of props and methods in an otherwise statically typed language - bringing in some of the abilities and better compatibility with dynamically typed languages such as e.g. Python.
like image 28
Thomas Weller Avatar answered Oct 07 '22 08:10

Thomas Weller