I have this code :
static dynamic Mean (dynamic x, dynamic y)
{
return (x + y) / 2;
}
static void Main()
{
int x = 3, y = 5;
Console.WriteLine (Mean (x, y)); //this is working
}
however the following line failes at runtime :
string s = Mean (3, 5); // Runtime error!
why is that ?
why s can not be set with the dynamic value ?
and if so , why Console.WriteLine (...) did succeed to run with this dynamic value ?
edit
and if if so , how can i make string s = Mean (3, 5); work ?
Because in this case you’re trying to set int value to your string variable.
Console.WriteLineautomatically do.ToString()before writing in console. Try this for example.