Click here to Skip to main content
15,888,273 members
Articles / Programming Languages / C#

Misconception of Dynamic Type Passed to Function and Type Returned

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
28 Jan 2014CPOL1 min read 4.9K   1  
Here is a misconception of dynamic type passed to function and type returned

This is a small post about misconception related to dynamic type variable supported by the C# language. Read more about dynamic keyword over here: Dynamic Types.

Check the below code:

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

According to written code, most developers think after writing the above code type of "withOffset" variable is type written by the function "DateTimeOffset.Parse", i.e., "DateTimeOffset".

Developer of the code thinks that compiler treats "var withOffset" as DateTimeOffset and all the method / property is available which is available for "DateTimeOffset" type is available for "withOffset" variable. But it's not true.

Image 1

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

The 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)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin 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:



Comments and Discussions

 
-- There are no messages in this forum --