I have the following script in my C # source:
class A{}
class Dispatch<T>{}
static class DispatchExt
{
public static void D<T>(this Dispatch<T> d, int a)
{
Console.WriteLine("Generic D chosen with a = " + a.ToString());
}
public static void D(this Dispatch<A> d, int a)
{
Console.WriteLine("D<A> chosen with a = " + a.ToString());
}
}
class Program
{
static void D<T>(Dispatch<T> d, int a)
{
d.D(a);
}
static void Main(string[] args)
{
int a = 5;
var dispatch = new Dispatch<A>();
dispatch.D(a);
D(dispatch, a);
}
}
When I run this code, the output is:
" D<A>selected with a = 5"
"General D, selected with a = 5"
This result surprised me because I expected it to be " D<A>selected with a = 5" in both situations.
I would like to know what are the general rules for resolving overload in this scenario or something that causes this conclusion. In addition, I am wondering if there is a way to achieve the first exit in both situations.
source
share