-
The compiler infers abstract class Base
{
public abstract T M<T>() where T : class;
}
class Derived : Base
{
public override T M<T>()
{
throw new NotImplementedException();
}
} When abstract class Base
{
public abstract T? M<T>() where T : class;
}
class Derived : Base
{
// ❌ CS0508 'Derived.M<T>()': return type must be 'T' to match overridden member
// ❌ CS0453 The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>'
// ↓
public override T? M<T>()
{
throw new NotImplementedException();
}
} Also, abstract class Base
{
public abstract T? M<T>() where T : struct;
}
class Derived : Base
{
public override T? M<T>()
{
throw new NotImplementedException();
}
}
I'm not finding the answer to this question in https://github.com/dotnet/csharplang/blob/main/meetings/2020/LDM-2020-06-17.md#t. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@cston may know the answer to this. |
Beta Was this translation helpful? Give feedback.
-
I think the problem is code like the following: #nullable enable
abstract class Base
{
public virtual void M<T>(T? x) where T : struct {}
public virtual void M<T>(T? x) where T : class {}
}
class Derived : Base
{
public override void M<T>(T? x) {}
} In But if it was possible to infer |
Beta Was this translation helpful? Give feedback.
I think the problem is code like the following:
In
Derived
,T?
is understood asNullable<T>
, sowhere T : struct
is inferred. That also means adding thewhere T : class
overload to the class after thewhere T : struct
overload is not a source breaking change.But if it was possible to infer
where T : class
, as you suggest, then adding thewhere T : struct
overload would be a source breaking change.