Click here to Skip to main content
13,193,816 members (46,222 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 28 Jan 2014

Misconception of Dynamic Type Passed to Function and Type Returned

, 28 Jan 2014
Rate this:
Please Sign up or sign in to vote.
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:

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.

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.



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


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 -
My CV :-


You may also be interested in...


Comments and Discussions

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