您可以对多个泛型类型参数实施相同的约束吗? [英] Can you enforce the same constraint on multiple generic type parameters?

查看:61
本文介绍了您可以对多个泛型类型参数实施相同的约束吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以这样操作:

I know you can you can do it like this:

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

我想要类似

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

是否有这种快捷方式?

推荐答案

否,不支持。 C#语言规范指出

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.

此处的键为 a ,表示语法要求,其中< TypeParam> :< Constraint1>,< Constraint2>等。

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

这篇关于您可以对多个泛型类型参数实施相同的约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆