You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicclassA{publicvirtualTFoo<T>()whereT:struct=>default(T)}publicclassB{publicvirtualTFoo<T>()whereT:struct=>base.Foo<T>;/* Error CS0460 Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly */}
This hinders readability of source code. If A is in a different assembly or package then B, it is not readily obvious that it is an error to pass a class to B.
Also when you inherit a class, you must specify the constraint again e.g.
publicclassA<T>whereT:struct{}publicclassB<T>:A<T>{}/*Error 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 'A<T>'*/
More over, the IL does require you to restate the constraint all though this is a moot point as C# does not always have to agree with IL.
I believe it should be optionally allowed to restate the constraint on method signatures. Code is read more than it is written, and being more explicit is good thing for code readability in my opinion. The current behavior should also still work.
I think he's missing some parenthesis there. Anyway, the proposal is that the compiler permit you to repeat the generic type constraint on an overridden member as long as it exactly matches, that way it is visible in the source. Currently it is forbidden for an overridden generic member to have generic type constraints at all. This seems to be the only place where generic type constraints are implicitly inherited.
@leppie That it does. I imagine he meant the following:
publicclassA{publicvirtualTFoo<T>()whereT:struct=>default(T)}publicclassB:A{publicvirtualTFoo<T>()whereT:struct=>base.Foo<T>();/* Error CS0460 Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly */}
Is it possible in the IL to have different generic constraints for a method override? That is why C# has you redeclare the constraints at the class level, i.e. to allow:
public class A<T> where T:IEquatable<T> {}
public class B<T> : A<T> where T:struct, IEquatable<T> {}
If not, if it's just a matter of readability, I disagree. It's already redundant enough to have to re-declare the entire signature, and optional things get enforced one way or the other via company coding guidelines so they're not really optional in practice.
No. The generic constraints are required to be the same. As far as I can tell they can't even be narrower. While I agree that it would be unnecessary to have to redeclare them I can understand the desire to strictly for clarity.
This discussion was converted from issue #353 on September 08, 2020 20:10.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
@mburbea commented on Fri Jan 22 2016
Currently, C# considers the following an error:
This hinders readability of source code. If
A
is in a different assembly or package thenB
, it is not readily obvious that it is an error to pass a class to B.Also when you inherit a class, you must specify the constraint again e.g.
More over, the IL does require you to restate the constraint all though this is a moot point as C# does not always have to agree with IL.
I believe it should be optionally allowed to restate the constraint on method signatures. Code is read more than it is written, and being more explicit is good thing for code readability in my opinion. The current behavior should also still work.
@leppie commented on Fri Jan 22 2016
Your first example makes no sense
@HaloFour commented on Fri Jan 22 2016
I think he's missing some parenthesis there. Anyway, the proposal is that the compiler permit you to repeat the generic type constraint on an overridden member as long as it exactly matches, that way it is visible in the source. Currently it is forbidden for an overridden generic member to have generic type constraints at all. This seems to be the only place where generic type constraints are implicitly inherited.
@leppie commented on Fri Jan 22 2016
@HaloFour: Not only that,
B
only derivesobject
.@HaloFour commented on Fri Jan 22 2016
@leppie That it does. I imagine he meant the following:
@asik commented on Fri Jan 22 2016
Is it possible in the IL to have different generic constraints for a method override? That is why C# has you redeclare the constraints at the class level, i.e. to allow:
If not, if it's just a matter of readability, I disagree. It's already redundant enough to have to re-declare the entire signature, and optional things get enforced one way or the other via company coding guidelines so they're not really optional in practice.
@HaloFour commented on Fri Jan 22 2016
@asik
No. The generic constraints are required to be the same. As far as I can tell they can't even be narrower. While I agree that it would be unnecessary to have to redeclare them I can understand the desire to strictly for clarity.
Beta Was this translation helpful? Give feedback.
All reactions