I was looking at this question and i was curious why this deosn't compile.
Given this code, can anyone explain why the call to IBase.Test()
doesn't resolve to the correct extension method?
public interface IBase { }
public interface IChildA : IBase { }
public interface IChildB : IBase { }
public static class BaseExtensions
{
public static IBase Test(this IBase self) { return self; }
public static T Test<T>(this T self) where T : IChildB { return self; }
}
public static class TestClass
{
public static void Test()
{
IChildA a = null; //Yeh i know its null but just testing for compile here..
IBase firstTry = a.Test(); //Cannot resolve to BaseExtensions.Test(this IBase obj)
IBase secondTry = ((IBase)a).Test(); //Resolves to BaseExtensions.Test(this IBase obj)
IChildB b = null;
IChildB touchedB = b.Test();
}
}
The error i get is
Error 166 The type 'IChildA' cannot be used as type parameter 'T' in the generic type or method 'BaseExtensions.Test<T>(T)'. There is no implicit reference conversion from 'IChildA' to 'IChildB'.
I had a feeling it is because it would be ambigous for anything that implements IChildB
and wouldn't know which extension method to use, but the error message doesn't moan about that side of it and if you remove the IBase firstTry = a.Test();
line then it compiles fine..