58

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
slash shogdhe
  • 3,943
  • 6
  • 27
  • 46
  • 5
    The meaning of "static" in that sentence is that you can't really "add" stuff (members) at runtime to whatever object is referenced by the `dynamic` reference (like you can in dynamically typed languages such as javascript, for example). – Federico Berasategui Aug 04 '14 at 17:59

2 Answers2

119

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.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • 22
    I now have an urge to name my classes based on what properties and methods they do *not* have. – Tim S. Aug 04 '14 at 20:09
  • 1
    @TimS. Well modern IDEs are so good at telling you exactly what a class *does* have, clearly they are missing telling you what they *don't* have. It's the new Hungarian notation! – Matt Burland Aug 04 '14 at 20:20
  • 5
    excellent answer, I feel like my brain has just expanded a little. – Dunno Aug 04 '14 at 21:16
  • In this `dynamic foo = new Foo(); foo.FooMethod();` case, will Intelisense be able to figure out what type `foo` is in order to complete `FooMethod()` for you? – Dan Is Fiddling By Firelight Aug 05 '14 at 13:09
  • 2
    @DanNeely: No. You lose intelisense when you use `dynamic`. – Matt Burland Aug 05 '14 at 13:11
  • Great explanation. Mind if I add a subtle question: is this any different from the old "Variant" type that VB always had? – Alpha Aug 07 '14 at 02:02
  • @Alpha: Not an expert on old VB, but from what I understand it is some what similar. – Matt Burland Aug 07 '14 at 03:14
  • What you're describing is "strong typing", which has nothing to do with this. The reason why it's says dynamic is a static type is because it is. If you have `dynamic x = 5;` the static type of the variable is `dynamic`. The runtime type is `int`. It's the same terminology as when you say; `IFoo foo = new Foo();` the static type is `IFoo`, the runtime type is `Foo`. This has nothing to do with whether you can add properties or not. – Neme Mar 26 '18 at 11:08
  • Even though it sounds a little funny, `dynamic` is a static type as far as the language is concerned. In fact it's a static type and a static type only - nothing can actually be of type `dynamic` at runtime. – Neme Mar 26 '18 at 11:12
1

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.

T_D
  • 1,688
  • 1
  • 17
  • 28
  • Strong/weak typing is a (somewhat subjective) distinction between statically typed languages. We say that C# is more strongly typed than C for example, but they are both statically typed. You can't really attribute strong or weak typing to dynamic languages like JavaScript, because the strong/weak distinction implies a static type system in the first place. – Theodoros Chatzigiannakis Aug 05 '14 at 09:04
  • 1
    @TheodorosChatzigiannakis: that's just wrong. Strong/weak typing and dynamic/static typing are orthogonal concepts. JS is well acknowledged as dynamic+weakly typed whereas Python is dynamic and strongly typed. Likewise, C is static/weak and C++ is static/strong. – mike3996 Aug 05 '14 at 12:18
  • @progo Alright. I definitely wasn't aware of any meaning of strong/weak in that context, until now. – Theodoros Chatzigiannakis Aug 05 '14 at 13:28