Why is C# dynamic type static?

C#C# 4.0Dynamic

C# Problem Overview


While reading and exploring the dynamic keyword I found following line on [MSDN] (in Using Type dynamic (C# Programming Guide)):

> The type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object.

What is the meaning of static in above line and how does it bypass static type checking?

C# Solutions


Solution 1 - C#

This is static typing:

string foo = "bar";

foo is now a string, so this will cause a compile time error:

foo = 1;

Even if you use var, it's still statically typed:

var foo = "bar";     // foo is now a string
foo = 1;             // still a compile time error

Using the dynamic keyword, means the type won't be static and can be changed, so now you can do this:

dynamic foo = "bar";   
foo = 1;              // this is now fine.

Now, why it says "the type is a static type" is because in many dynamic languages (like Javascript), you can do something like this:

var foo = { bar: 1 };

Which creates an object with a property called "bar", and then you can do this:

foo.la = 2;

Which adds a new property to the object in foo. But if you try the same trick in C#

dynamic foo = new SomeClassThatDoesntHaveABarProperty();
foo.bar = 2;          // runtime error

Nor can you delete a property. You can assign any type to a dynamic variable, but you can't change those types themselves.

If you do need that type of functionality, then you'll want to look at ExpandoObject

As your description says, dynamic functions like an object in a lot of cases. You could do this:

dynamic foo = new Foo();
foo = new Bar();

Just as well like this:

object foo = new Foo();
foo = new Bar();

But the difference comes in when you want to use properties or methods. With dynamic, I can do this:

dynamic foo = new Foo();
foo.FooMethod();          // Note: You WILL get a runtime exception if foo doesn't have a FooMethod

But with an object, I'd need to do this:

object foo = new Foo();
((Foo)foo).FooMethod();    // Note: I HAVE to know the type at compile time here

Which I can only do if I already know I can cast the type in foo to a type of Foo at compile time, and if I knew that already, then I could just have use Foo as my type instead of object.

Solution 2 - C#

It means that a variable declared as dynamic will stay of type dynamic and cannot be changed to a variable of type int for example. This concept is bypassed though, because you can change the types of objects that the variable holds.

C# is considered as a strongly typed language because variables are statically typed. That means every variable is typed and the C#-compiler can check if the right types are used in the code. In weakly typed language like most script languages the type of variables is dynamic. They can hold any value.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionslash shogdheView Question on Stackoverflow
Solution 1 - C#Matt BurlandView Answer on Stackoverflow
Solution 2 - C#T_DView Answer on Stackoverflow