I use C# 10 and I have an generic abstract class. This abstract class implemented by 2 other classes which one uses reference type and the other uses value type.
public abstract class BaseClass<T> {
public abstract T? AbstractNullableTestMethod();
public abstract T AbstractNotNullableTestMethod();
}
public class A : BaseClass<int> {
public override int AbstractNotNullableTestMethod() => throw new NotImplementedException();
public override int? AbstractNullableTestMethod() => throw new NotImplementedException();
}
public class B : BaseClass<string> {
public override string AbstractNotNullableTestMethod() => throw new NotImplementedException();
public override string? AbstractNullableTestMethod() => throw new NotImplementedException();
}
In this example, I get an error for class A. Because int is not null so I get return type must be int error for AbstractNullableTestMethod method. If I add where T : struct constraint this time class B get error because string is reference type.
The abstract class has multiple method, some of nullable, the others not. However, it can be implemented by reference type and value type generics.