The program below produces this output:
Foo<T> called
Process is terminated due to StackOverflowException.
So, Foo(baz)
calls the generic Foo<T>
, but Bar(baz)
recurses and does not call Bar<T>
.
I'm on C# 5.0 and Microsoft .NET. The compiler seems to choose the generic method, instead of recursion, when the non-generic method is an override
.
Where can I find an explanation for this rule? (I had guessed that the compiler would choose recursion in both cases.)
Here is the program in its entirety:
using System;
namespace ConsoleApplication1 {
class Baz { }
abstract class Parent {
public abstract void Foo(Baz baz);
}
class Child : Parent {
void Bar<T>(T baz) {
Console.WriteLine("Bar<T> called");
}
public void Bar(Baz baz) {
Bar(baz);
}
void Foo<T>(T baz) {
Console.WriteLine("Foo<T> called");
}
public override void Foo(Baz baz) {
Foo(baz);
}
}
class Program {
static void Main(string[] args) {
var child = new Child();
child.Foo(null);
child.Bar(null);
Console.ReadLine();
}
}
}