I'm trying to call overloaded method in code like this:
public abstract class BaseClass<T>
{
public abstract bool Method(T other);
}
public class ChildClass : BaseClass<ChildClass>
{
public bool Method(BaseClass<ChildClass> other)
{
return this.Method(other as ChildClass);
}
public override bool Method(ChildClass other)
{
return this == other;
}
}
class Program
{
static void Main(string[] args)
{
BaseClass<ChildClass> baseObject = new ChildClass();
ChildClass childObject = new ChildClass();
bool result = childObject.Method(baseObject);
Console.WriteLine(result.ToString());
Console.Read();
}
}
Everything looks ok, but the StackOverflowException is thrown.
In my understanding if i call overloaded method, then the most specific method version should be called, but in this case the Method(BaseClass<ChildClass> other) is called instead of Method(ChildClass other).
But when i use a cast:
return ((BaseClass<ChildClass>)this).Method(other as ChildClass);
everything works as expected. Am i missing somethig? Or this is a bug in .NET? Tested in .NET 2.0,3.5,4.0