CA1005: Avoid excessive parameters on generic types

CA1005: Avoid excessive parameters on generic types

https://docs.microsoft.com/en-us/visualstudio/code-quality/install-roslyn-analyzers?view=vs-2019 需要先进行安装

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 more than two type parameters exist, the difficulty becomes too great for most users (for example, TooManyTypeParameters<T, K, V> in C# or TooManyTypeParameters(Of T, K, 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 suppress warnings

Do not suppress a warning 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.

 

Types and methods should not have too many generic parameters

A method or class with too many type parameters has likely aggregated too many responsibilities and should be split.

Noncompliant Code Example

With the default parameter value of 2:

<S, T, U, V> void foo() {} // Noncompliant; not really readable
<String, Integer, Object, String>foo(); // especially on invocations

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(229)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2015-06-09 C#和.net之间的关系
2015-06-09 Visual C#每一次新版本的变化
2014-06-09 VS中sln和suo的区别
点击右上角即可分享
微信分享提示