IN
A x = Foo("foobar");
C # chooses a generic method because it is more specific than not generic and does not require conversion. In fact, the C # compiler creates a copy of the Foo method and replaces the parameter of type type T concrete type string . Overload resolution is performed at compile time. At run time, a method with a string parameter is called. At run time, no common overhead is generated.
Note that for resolution only the expression on the right side of the assignment is taken into account. More specifically, C # considers the signature of the method, i.e. the parameters of the method. The type of the returned method is not related to its signature.
The general method returns A<T> , but since A<T> not inferred from A , the result of type A<T> method Foo<T>() cannot be assigned x , which is of type A The same is true for the dynamic example: there is no valid conversion from A<T> to A Since overload resolution is performed during compilation, the speaker cannot solve your problem. Dynamics does its βworkβ (ie, Binding) at run time.
Again, this is not the result you expect from a method that determines which overload is used, but the (static) arguments passed to this method.
Another example that helps clarify the facts:
var x = Foo(5); var y = Foo("hello");
At compile time, C # creates two copies of the Foo method! One with int and one with string instead of the type parameter parameter T No conversion occurs at run time; not even boxing (unlike Java, which would wrap an int in an object).
source share