Dynamic parameter issues

I have this function

string F(dynamic a) { return "Hello World!"; } 

later when i say

 dynamic a = 5; var result = F(a); 

the result should be a string type at compile time, but it is not, why? Actually match it

 int result2 = F(a); 

but not

 int result3 = F(5); 

Anything help?

+5
source share
1 answer

This is explained in here :

Overload resolution occurs at run time, and not at compile time, if one or more of the arguments in the method call is of dynamic type or if the receiver of the method call is of type dynamic.

Now in the case of F(a) , since a is dynamic, the compiler does not check overloads at compile time. But when you say:

 F(2); 

2 is an integer, not dynamic . That is why overload resolution occurs at compile time, and you get an error. If you pass an integer literal to dynamic, you will not get any error at compile time (but you do this at runtime):

 int x = F((dynamic)2); 
+6
source

Source: https://habr.com/ru/post/1206346/


All Articles