Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is C# dynamic type static?

Tags:

c#

dynamic

c#-4.0

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?

like image 253
slash shogdhe Avatar asked Aug 04 '14 17:08

slash shogdhe


People also ask

Why is C so used?

The C programming language doesn't seem to have an expiration date. It's closeness to the hardware, great portability and deterministic usage of resources makes it ideal for low level development for such things as operating system kernels and embedded software.

What is called C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

Why C is called simple language?

If you write a C code in your machine, it will run on any machine which supports C, without modifying a single line of code. Because it is not tied to any hardware or system. We can say, it is a hardware independent language or platform independent language. That is why c is called a portable language.


1 Answers

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.

like image 169
Matt Burland Avatar answered Oct 16 '22 22:10

Matt Burland