I am new to the C # dynamic keyword. In one of my projects, I tried to play with him and came across some unexpected behavior. I was able to reproduce the situation with the following code:
class Program { static DateTime? DateOnly(DateTime? time) { return time.HasValue ? (System.DateTime?)time.Value.Date : null; } static void Main(string[] args) { dynamic now = System.DateTime.Now; var date = DateOnly(now); Console.WriteLine(date.Value);
I get a RuntimeBinderException message
'System.DateTime' does not contain a definition for 'Value'.
So, is the date variable treated as DateTime instead of DateTime? .
It seems like dynamic somehow ignoring the declaration of return type. Should I avoid using var with dynamic ?
source share