Pensavo che il metodo che viene chiamato sia deciso al runtime o mi sia sfuggito qualcosa? Esempio di codice:Perché non viene richiamato il metodo sovraccaricato?
class Program
{
static void Main(string[] args)
{
var magic = new MagicClass();
magic.DoStuff(new ImplA());
magic.DoStuff(new ImplB());
Console.ReadLine();
}
}
class MagicClass
{
internal void DoStuff<T>(T input) where T : SomeBase
{
HiThere(input);
}
void HiThere(SomeBase input)
{
Console.WriteLine("Base impl");
}
void HiThere(ImplA input)
{
Console.WriteLine("ImplA");
}
void HiThere(ImplB input)
{
Console.WriteLine("ImplB");
}
}
abstract class SomeBase
{
}
class ImplA : SomeBase{}
class ImplB : SomeBase{}
ho pensato di ottenere:
ImplA
ImplB
come output, ma la stampa Base impl
. C'è qualcosa che posso fare per ottenere il metodo sovraccarico senza trasmettere l'input?
Ecco una spiegazione abbastanza decente perché: http://csharpindepth.com/Articles/General/Overloading.aspx –