Is it possible to let an overridden method from a base class to return the type of the derived class?
What I mean is:
namespace test //with some "pseudo" code
{
class Base<T>
{
//fields ...
//properties ...
// ...
public virtual Base<T> BaseMethod()
{
//return new Base<T>;
}
// ...
}
class DerivedfromBase : Base<int>
{
// ...
public override Base<int> BaseMethod()
{
//other things here
return // a Base<int> type;
//Can it return DerivedfromBase type here?
}
// ...
}
// Main
{
DerivedfromBase AA = new DerivedfromBase();
//will compile, but run will give invalid cast exeption
DerivedfromBase BB = (DerivedfromBase)AA.BaseMetod();
}
//}
Tried different things myself, but as I'm a still a learner and could find nothing useful on the web, I'm a bit stuck.
Hope someone can help me out here and thanks in advance. :)