Any limit to number of properties on a .NET Class?

2019-03-17 22:13发布

Received a spec to add over 800 properties to an object. Is their any 'limits' to the number of Properties an object can have in C# (or .NET)?

Is their any performance impacts to be concerned with in regards to objects of this class with this many properties?

Thanks!

标签: c# .net oop
1条回答
狗以群分
2楼-- · 2019-03-17 23:01

The metadata can have up to 24-bit references/definitions per assembly. Being a property, you need 2 methods per property. Hence the limit will be 23-bit, or 1 << 23 - 1 for the entire assembly.

Update:

If they are only read-only properties, the limit would be 1 << 24 - 1.

Answer to second question:

No, there will be no performance overhead. Simple properties are likely to be inlined by the JIT.

Some thoughts:

You will never reach the above limit. Imaging having 16 million properties. That will require 16 million strings stored for the names too. Say the average name is 8 chars, then you are looking at a string table size of ~256MB (property name + method name), and then you havent even started coding yet. Just a thought.

查看更多
登录 后发表回答