Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Misconception of Dyanmic type passed to function and type returned

, 28 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Small post is about miconception related to dynamic type variable supported by the C# langauge. Read more about dynamic keyword over here : Dynamic TypesCheck below code dynamic str = "22/11/2013 10:31:45 +00:01";var withOffset = DateTimeOffset.Parse(str);According to written code most of the devel
Small post is about miconception related to dynamic type variable supported by the C# langauge. Read more about dynamic keyword over here : Dynamic Types

Check below code
dynamic str = "22/11/2013 10:31:45 +00:01";
var withOffset = DateTimeOffset.Parse(str);

According to written code most of the developer thinks after wiriting above code type of "withOffset" variable is type written by the function "DateTimeOffset.Parse" i.e. "DateTimeOffset".

Devloper of the code thinks that compiler treat "var withOffset" as DateTimeOffset and all the method / property is avaiable which is avaialbe for "DateTimeOffset" type is avaible for "withOffset" variable. But its not true.



So question get raised why the type of not get changed to return type of the function.

Answer is :
When you use dynamic, the entire expression is treated at compile time as a dynamic expression, which causes the compiler to treat everything as dynamic and get run-time binding.

This is explained in 7.2 of the C# Language specification:
When no dynamic expressions are involved, C# defaults to static binding, which means that the compile-time types of constituent expressions are used in the selection process. However, when one of the constituent expressions in the operations listed above is a dynamic expression, the operation is instead dynamically bound.
This basically means that most operations (the types are listed in section 7.2 of the spec) which have any element that is declared as dynamic will be evaluated as dynamic, and the result will be a dynamic.

Since the argument is dynamic, the compiler cannot know which method will be called at runtime. Therefore, it cannot know what the return type will be. We might know that the return type will be DateTimeOffset, but the compiler does not, and cannot, know that.
Reference : http://stackoverflow.com/questions/20150687/c-sharp-dlr-datatype-inference-with-dynamic-keyword

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

 
Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 

You can visit my blog

StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 28 Jan 2014
Article Copyright 2014 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid