开发者

Limit on amount of generic parameters in .NET?

开发者 https://www.devze.com 2022-12-24 01:55 出处:网络
Is there a limit on the amount of generic parameters you can h开发者_运维问答ave on a type in .NET? Either hard limit (like 32) or a soft limit (where it somehow effects performance to much, etc.)

Is there a limit on the amount of generic parameters you can h开发者_运维问答ave on a type in .NET? Either hard limit (like 32) or a soft limit (where it somehow effects performance to much, etc.)

What I'm referring to is:

class Foo<T0, T2, T3, T4, etc.> {
}


From the C# 2.0 language spec

8.16.3 Multiple type parameters Generic type declarations can have any number of type parameters.


Anonymous types in C# 3.0 are actually generic, for reasons which I should probably blog about at some point. When we designed anonymous types we realized that of course people could be creating anonymous types with potentially hundreds of fields, so we did a lot of testing of the performance of generics with lots of type parameters.

We didn't find any notable problems.

However, what we consider acceptable, you might not. My advice: try it and see. Write up some benchmarks, execute them, and then you'll be reasoning from empirical data, rather than reasoning from the guesses of random people on the internet who don't know what your user scenarios are or what performance factors are important to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消