20

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!

CmdrTallen
  • 2,264
  • 4
  • 28
  • 50

2 Answers2

40

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
leppie
  • 115,091
  • 17
  • 196
  • 297
  • Doesn't the property itself need a metadata slot too? Which would result in something like 2^24/3 – CodesInChaos Jun 10 '11 at 13:14
  • 1
    @CodeInChaos: Different tables :) – leppie Jun 10 '11 at 13:16
  • 12
    Nice to see an answer which is based in actually knowing the answer, rather than vague "I don't think there is a limit, but that is sure bad design" ones. +1 – jalf Jun 10 '11 at 13:19
  • @Jodrell: Me? About 25wpm. After almost a decade of coding, I still cant touch type :( – leppie Jun 10 '11 at 14:44
  • 2
    So, assuming 8 hour days and no slacking and that you use the prop snippet so only have to type about 4 words per property... 2796 working days to get there. – Jodrell Jun 10 '11 at 16:35
1

I do not think the accepted answer is correct. We have just hit a problem with our assembly that adding any new property does build the code, but trying to load the assembly with reflection results in File is corrupt. HRESULT 0x8013110E

Removing any property after that allows to load the assembly with reflection again.

I obtained the metadata stats with ildasm:

C:\temp> ildasm.exe xyz.dll /stats /out=c:\temp\1.txt
C:\temp> cat C:\temp\1.txt | sls '\bFieldDef\b'

//   FieldDef      - 65534 (655340 bytes) 181 constant

Looks like a pretty clear 16 bits limit on FieldDef.

mark
  • 59,016
  • 79
  • 296
  • 580