Can you enforce the same constraint on multiple generic type parameters?

匆匆过客 提交于 2019-12-04 06:32:18

问题


I know you can you can do it like this:

void M<T1, T2, T3>() where T1 : S where T2 : S where T3 : S 
{}

I want something like this:

void M<T1, T2, T3>() where T1, T2, T3 : S 
{}

Is there any such shortcut?


回答1:


No, that is not supported. The C# language spec states

Each type-parameter-constraint-clause consists of the token where, followed by the name of a type parameter, followed by a colon and the list of constraints for that type parameter.

The key here is a, indicating that the grammer requires where <TypeParam> : <Constraint1>,<Constraint2>, etc.. .



来源:https://stackoverflow.com/questions/18290682/can-you-enforce-the-same-constraint-on-multiple-generic-type-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!