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!

7
  • I'm sure there is some limit. But I assume it's much higher than 1000. Jun 10, 2011 at 13:15
  • 1
    I would love to know what your architect did before he became an architect. Maybe a musician playing 200 instruments at the same time and did it perfectly?
    – jgauffin
    Jun 10, 2011 at 13:16
  • 10
    How do you name these? Please dont say Property001 -> Property800
    – leppie
    Jun 10, 2011 at 13:18
  • 2
    Can you explain what kind of data this class should hold, and why somebody thought that having this many properties is a good idea? Jun 10, 2011 at 13:18
  • 8
    @leppie - Don't be ridiculous. Surely Property000 to Property799 would be better style.
    – MikeJ-UK
    Jun 10, 2011 at 14:46

2 Answers 2

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.

5
  • Doesn't the property itself need a metadata slot too? Which would result in something like 2^24/3 Jun 10, 2011 at 13:14
  • 1
    @CodeInChaos: Different tables :)
    – leppie
    Jun 10, 2011 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, 2011 at 13:19
  • @Jodrell: Me? About 25wpm. After almost a decade of coding, I still cant touch type :(
    – leppie
    Jun 10, 2011 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, 2011 at 16:35
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.