-5
var foo1 = new SomeClass();
SomeClass foo2 = new SomeClass();

Does those two lines of code translate into the same IL, and how does the dynamic differ from them ? I know that dynamic uses some kind of technique called late binding but how does that work ?

Dimitri
  • 2,798
  • 7
  • 39
  • 59
  • 3
    This looks like a job for... the Skeet. – christopher Jul 29 '13 at 07:40
  • 1
    They are the same, there is no difference in IL. Question asked and answered many times. – h.alex Jul 29 '13 at 07:42
  • Is your question actually about `var` (as per the title) or `dynamic` (as per the body)? They're completely different - and both are extensively documented. – Jon Skeet Jul 29 '13 at 07:42
  • I find lots of explanations of what `dynamic` does this after typing `dynamic c#` into my favourite search engine. Perhaps you could be more specific about which bits you don't understand? – Philip Kendall Jul 29 '13 at 07:42

1 Answers1

8

Does those two lines of code translate into the same IL?

Yes

and how does the dynamic differ from them ?

Meaningless until you display member access; for example:

dynamic foo3 = new SomeClass();
foo3.SomeMethod();

Here, SomeMethod is resolved entirely at runtime via either a dynamic despatch layer or a cached meta-programming layer that sits on top of reflection (depending on what capabilities SomeClass has). In regular (non-dynamic) IL, it would just be a single callvirt opcode.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900