[FxCop.设计规则]4. 避免太多的范型参数

4.     避免太多的范型参数

原文引用:

Avoid excessive parameters on generic types

TypeName:

AvoidExcessiveParametersOnGenericTypes

CheckId:

CA1005

Category:

Microsoft.Design

Message Level:

Error

Certainty:

85%

Breaking Change:

Breaking


Cause: An externally visible generic type has more than two type parameters.

Rule Description

The more type parameters a generic type contains, the more difficult it is to know and remember what each type parameter represents. It is usually obvious with one type parameter, as in List<T>, and in certain cases with two type parameters, as in Dictionary<TKey, TValue>. If there are more than two type parameters, the difficulty becomes too great for most users, for example, TooManyTypeParameters<T, K, V> in C# or TooManyTypeParameters(Of T, Of K, Of V) in Visual Basic.

How to Fix Violations

To fix a violation of this rule, change the design to use no more than two type parameters.

When to Exclude Messages

Do not exclude a message from this rule unless the design absolutely requires more than two type parameters. Providing generics in a syntax that is easy to understand and use reduces the time that is required to learn and increases the adoption rate of new libraries.

Related Rules

Collections should implement generic interface

Do not declare static members on generic types

Do not expose generic lists

Do not nest generic types in member signatures

Generic methods should provide type parameter

Use generic event handler instances

Use generics where appropriate

See Also

Generics DesignGuidelines

引起的原因:

一个输出的范型类型拥有多于2个范型参数。

描述:

如果一个范型类拥有太多类型参数,使用它就会变得很困难,你必须了解和记住每一个参数的意义。这样明显不如一个类型参数的范型类(例如:List<T>)或在某些必要的场景中的两个参数的范型类(例如:Dictionary<TKey,TValue>)。如果使用多于两个参数,对于用户来说,使用难度就会变得很大,例如:TooManyTypeParameters<T, K, V>

修复:

修复这个问题需要修改设计,使用少于两个参数的范型类。

例外:

除非设计必须使用多于两个类型参数的范型类型,尽量避免违反这条规则。Providing generics in a syntax that is easy to understand and use reduces the time that is required to learn and increases the adoption rate of new libraries.这句话实在不会翻译了!L

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值