Does C# pick the wrong type for var when parsing a dynamic object?

后端 未结 4 804
刺人心
刺人心 2020-12-16 16:51

I am using the following code to convert some Json into a dynamic object. When I use DateTime.Parse on a property of my dynamic type I would expect the var to guess that it\

4条回答
  •  -上瘾入骨i
    2020-12-16 17:14

    I don't think this is particularly surprising.

    DateTime.Parse() will evaluate to dynamic.

    DateTime startDate = does a runtime assignment from a dynamic to a DateTime.

    You've just combined the two.

    The compiler isn't guessing the type of DateTime.Parse() to be anything other than dynamic, but it's clever enough to realise that if you do an assignment of this value to a DateTime then assuming it is successful you're left with a DateTime.

提交回复
热议问题