Generic field in subclass with type parameter of subclass

Suppose I have the following situation (the ??? element is the unknown subject of my question):

abstract class Base
    private Generic<?????> fieldReference;

abstract class Generic<T> where T : Base {

class BaseExtended : Base {
    // fieldReference in this class should have type Generic<BaseExtended>

class GenericExtended : Generic<BaseExtended> {

I would like the field fieldReference of Base to have the generic type parameter of whatever type has extended Base (I’ve written ??? because I don’t know what is supposed to go here, if it is even possible). Since this type is guaranteed to extend Base, and is known at compile time for each class that extends Base, it seems like I should be able to achieve this without creating any traps.

Is there a way to do this in the language, and if not, why (what does it break that I’m not thinking of)?

That’s not possible. Keep in mind that a generic type declaration is incomplete. It can not be used until the type arguments are bound to an actual type. Since you want your Base class to be non generic it can’t have a type argument that isn’t specified.

Keep in mind that inheritance means that a derived class actually is also a base class instance. So in your case when you create an instance of “BaseExtended” it also is a Base instance. So you can do

Base inst = new BaseExtended();

However from the Base perspective where you actually defined your field, the field type can not change because the Base class isn’t aware of what derived class it might be.

Circular generic type references aren’t possible. You may want to do something like this

Note that generics in .NET / Mono are not the same as templates in C++ (they are actually very different). A generic class has type arguments which are actually bound at runtime, not at compile time. A class has to complile without knowing the actual type.

Also note that a generic class with two different type arguments are two completely different classes. They have nothing in common. That’s why a List<string> and a List<int> are two seperate concrete classes which share absolutely no common class (besides System.Object). Since your Base class is not a generic class you have to give an actual type argument for your “fieldReference” type. However that type would be final. If the type should be able to be changed then the Base class need to be generic as well.