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!
Properties are the special type of class members that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors.
The correct answer, in the current CLR implementation, is ushort. MaxValue - 15.
Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code. A get property accessor is used to return the property value, and a set property accessor is used to assign a new value.
A property is a value or characteristic held by a Visual Basic object, such as Caption or Fore Color. Properties can be set at design time by using the Properties window or at run time by using statements in the program code. Object is the name of the object you're customizing.
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. Imagine 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With