views:

2119

answers:

6

When C# 4.0 comes out and we have the dynamic keyword as described in this excellent presentation by Anders Hejlsberg, (C# is evolving faster than I can keep up.. I didn't have much time to acquaint myself with the var keyword)

Would I still need the var keyword ? Is there anything that var can do.. that dynamic can't?

var x = SomeFunctionThatIKnowReturnsSomeKindOfList();
// do something with x

dynamic x = SomeFunctionThatIKnowReturnsSomeKindOfList();
// do something with x
+55  A: 

No, they're very different.

var means "infer the type of the variable at compile-time" - but it's still entirely statically bound.

dynamic means "assume I can do anything I want with this variable" - i.e. the compiler doesn't know what operations are available, and the DLR will work out what the calls really mean at execution time.

I expect to use dynamic very rarely - only when I truly want dynamic behaviour:

  • var lets you catch typos etc at compile-time
  • statically bound code is always going to run faster than dynamically bound code (even if the difference becomes reasonably small)
  • statically bound code gives more compile-time support beyond just errors: you can find call hierarchies, refactoring will work better, Intellisense is available etc
Jon Skeet
That was my missing piece.. var is static-binding (and hence comes with the benefits of compile-time checks, tooling, etc.)
Gishu
you can basically think of var as some syntactic sugar
borisCallens
+15  A: 

Dynamic and var represent two completely different ideas.

var

Var essentially asks the compiler to figure out the type of the variable based on the expression on the right hand side of the assignment statement. The variable is then treated exactly as if it were explicitly declared as the type of the expression. For example the following two statements are equivalent

var a = "foo";
string a = "foo";

The key to take away here is that "var" is 100% type safe and is a compile time operation

dynamic

Dynamic is in many ways the exact opposite of var. Using dynamic is essentially eliminating all type safety for thet particular variable. It many ways it has no type. When you call a method or field on the variable, the determination on how to invoke that field occurs at runtime. For example

dynamic d = SomeOperation();
d.Foo(); // Will this fail or not?  Won't know until you run the program

The key to take away here is that "dynamic" is not type safe and is a runtime operation

JaredPar
It's still type safe, its just that the type isn't known at compile time. If the actual (runtime) object doesn't have a particular member you're trying to access, you'll get exception.
Isak Savo
+1  A: 

Yes you will still need var:

Var is a variable whose type will be inferred by the compiler.
dynamic will have its type assigned at runtime

So:

Var i = "Hello World"

will have its type inferred as a string type in doing so intellisence will give you all the methods that string can use like,

i.Split("/")

Where as:

dynamic i = "Hello World"

won't have its type inferred untill runtime because the complier dosn't know what type it is yet, but will still let you do:

i.Split("/")

but when it calls the method that you need it may fail because the type is wrong and the method isn't there.

Nathan W
Pedant: "var" is a *variable* whose type will be inferred by the compiler. The object is unaffected. "dynamic" is a variable that is statically typed as "dynamic" - it is the dispatch mechanism that changes (the variable is still typed as "dynamic").
Marc Gravell
A: 

I have a funky question - can a variable, which is statically typed to be dynamic (like Anders Hejlsberg says), be inferred at the compile time?

var foo = new Microsoft.Office.Interop.Excel.Application(); // foo is inferred at the compile time to be a type from Microsoft.Office.Interop.Excel namespace. It is not dynamic, isn't it?
var bar = foo.Cells[1,1]; // Up to C# 3.0 bar was an object. Will it be in C# 4.0?
bar.Value2 = "something"; // Or in C# 4.0 bar is dynamic, so I can write something like this, instead of downcasting or invoking a property by reflection mechanism?
You'll get more traction asking this as a separate question.
Bevan
A: 

So it 'dynamic' basically like declaring something of type "object" but the compiler does not complain, when you call methods on it that do not exist?

Hermann
Yes it will be resolved at run-time. If it exists, the method will be called.. if not Boom!
Gishu