Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between dynamic (C# 4) and var?

Tags:

c#

dynamic

I had read a ton of articles about that new keyword that is shipping with C# v4, but I couldn't make out the difference between a "dynamic" and "var".

This article made me think about it, but I still can't see any difference.

Is it that you can use "var" only as a local variable, but dynamic as both local and global?

Could you show some code without dynamic keyword and then show the same code with dynamic keyword?

like image 848
Ivan Prodanov Avatar asked Jun 07 '09 09:06

Ivan Prodanov


People also ask

What is difference between dynamic and static in C?

Static Memory Allocation is done before program execution. Dynamic Memory Allocation is done during program execution. In static memory allocation, once the memory is allocated, the memory size can not change. In dynamic memory allocation, when memory is allocated the memory size can be changed.

What is Dynamic C programming?

A Dynamic C program is a set of files consisting of one file with a . c extension and the requested library files. Each file is a stream of characters that compose statements in the C language. The language has grammar and syntax, that is, rules for making statements.

What is a dynamic variable in C?

A dynamic variable can be a single variable or an array of values, each one is kept track of using a pointer. After a dynamic variable is no longer needed it is important to deallocate the memory, return its control to the operating system, by calling "delete" on the pointer.

What is difference between static and dynamic variable?

Static variables (should) remain the same e.g. temperature of a water bath, k constant of a particular spring. Dynamic variables change as the experiment progresses e.g. air temperature and pressure, amount of natural light.


1 Answers

var is static typed - the compiler and runtime know the type - they just save you some typing... the following are 100% identical:

var s = "abc"; Console.WriteLine(s.Length); 

and

string s = "abc"; Console.WriteLine(s.Length); 

All that happened was that the compiler figured out that s must be a string (from the initializer). In both cases, it knows (in the IL) that s.Length means the (instance) string.Length property.

dynamic is a very different beast; it is most similar to object, but with dynamic dispatch:

dynamic s = "abc"; Console.WriteLine(s.Length); 

Here, s is typed as dynamic. It doesn't know about string.Length, because it doesn't know anything about s at compile time. For example, the following would compile (but not run) too:

dynamic s = "abc"; Console.WriteLine(s.FlibbleBananaSnowball); 

At runtime (only), it would check for the FlibbleBananaSnowball property - fail to find it, and explode in a shower of sparks.

With dynamic, properties / methods / operators / etc are resolved at runtime, based on the actual object. Very handy for talking to COM (which can have runtime-only properties), the DLR, or other dynamic systems, like javascript.

like image 59
Marc Gravell Avatar answered Sep 30 '22 12:09

Marc Gravell