I have the following code in C#:
Action a = new Action(() => Console.WriteLine());
dynamic d = a;
d += "???";
Console.WriteLine(d);
and the output is
System.Action???
while if you add an int instead of a string to d it would throw an exception.
Can you please explain why does this happen?
I think this happens because when you use
d += "???";d is converted to string (using defaultToString()method which takes object name) and then “???” is appended and wrote to console.If you try to use
d += 2this fails because there’s no default way to convert an Action to an integer. Same for other types…