dynamic d = 5;
var array = new[] {d,"hello"}
What is the implicit type of array
? It is dynamic[]
but not string[]
, why ?
While going through C# in depth - Jon Skeet stated a rules for dynamic conversions :
An implicit conversion exists from any expression of type dynamic to almost any CLR type
Later on he mentioned that he purposely said conversion from an "expression of type dynamic" but not from dynamic type itself.
You may have also noticed that I wrote about a conversion “from an expression of type dynamic” to a CLR type, not a conversion from the dynamic type itself. This subtlety helps during type inference and other situations that need to consider implicit conversions between types
I am bit confused or probably lacking something very basic, but this explains why array
type turned out to be dynamic[]
and not string[]
. Can someone help me understand what he actually meant.
dynamic d = 5; var array = new[] { "hello", d };
on whyarray
is of typedynamic[]
and notstring[]
helps to understand the intent.