Concatenating a string action using dynamics

I have the following code in C #:

Action a = new Action(() => Console.WriteLine());
dynamic d = a;
d += "???";
Console.WriteLine(d);

and the way out is

System.Action ???

and if you add int instead of a string in d, it will throw an exception.

Could you explain why this is happening?

+5
source share
2 answers

, , , d += "???";, d ( ToString(), ), "???" .
d += 2, , . ...

+3

string - .NET .ToString, . , dynamic.

Action a = new Action(() => Console.WriteLine());
Console.WriteLine(a + "???"); // outputs "System.Action???"

Action System.Action, .ToString.

+= + , . :

object a = new Action(() => Console.WriteLine());
a = a + "???"; // The same as: a = a.ToString() + "???";
Console.WriteLine(a);
+2

All Articles