Reputation: 353
Can someone explain to me the difference between these two string variables :
dynamic dateStrAsDynamic = "12/10/2013";
var dateStrFromDynamic = dateStrAsDynamic.ToString();
var regularDateStr = "12/10/2013";
These two behave exactly the same way but while debugging calling DateTime.Parse
on the first one tells me that this dynamic operation is not supported while debugging, I mean "which dynamic operation?", whatever dynamic operation it is; must it not have been over?
The IL code generated by calling DateTime.Parse
on those two (of course after ToString
is called and assigned to dateStrFromDynamic) have a large difference as well that I am not able to grasp totally.
Do these two really have a difference, or am I missing something?
Upvotes: 3
Views: 1838
Reputation: 1502286
I mean "which dynamic operation?"
The one that invokes a method using a dynamic variable as the argument.
Note that the type dateStrFromDynamic
will still be dynamic
- the compiler doesn't know that ToString()
will definitely return a string
. The result of almost any dynamic operation is another dynamic value - if you want to tell the compiler that you want the type of dateStrFromDynamic
to be string
, you need to make that explicit:
string dateStrFromDynamic = dateStrAsDynamic.ToString();
Or just:
string dateStrFromDynamic = dateStrAsDynamic;
(given that it really is a string to start with).
So yes, there is an enormous difference between dateStrFromDynamic
and regularDateStr
- and if you hover over var
in Visual Studio, it will become more obvious as it will tell you the type that the compiler has inferred for each variable.
Upvotes: 8