Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

oop

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!

like image 293
CmdrTallen Avatar asked Jun 10 '11 13:06

CmdrTallen


People also ask

What are properties of a class in C#?

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.

How many methods can a class contain C#?

The correct answer, in the current CLR implementation, is ushort. MaxValue - 15.

Why do we need properties in C#?

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.

What are class properties in VB net?

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.


1 Answers

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.

like image 182
leppie Avatar answered Sep 21 '22 19:09

leppie