Will this class declaration resolve?

public abstract class SomeClass<T> where T : SomeClass<T>
{
    protected T derivedRef;

    // ...
}
public class DerivedVersion1 : SomeClass<DerivedVersion1>
public class DerivedVersion2 : SomeClass<DerivedVersion2>

The desired result is to have one abstract class that different Derived : SomeClass can inherit from and the methods that accept type Derived can have customized behavior specific to the Derived type without casting.

I’m not getting any warnings or errors from the compiler, but I’m curious if this will resolve properly, or if it can be resolved at all? Just writing it feels like it will cause a cycle of trying to resolve T infinitely.

Edit: Whoops, I should have asked on SO, not here. Sorry guys!

That works perfectly fine and is used by many.

1 Like