1

I was reading into the nitty-gritty of structs in C# here: "C Fundamentals, The differences between struct and class." and at the very bottom it mentions keeping a struct below 16 bytes. I looked elsewhere and saw that it is commonly recommended, though it sounds like it's been bumped up a bit to 20-24 bytes here in later versions of .NET.

My question is, are they just adding up the ints, doubles, etc. to total the 16 bytes, or are they talking about the size after compile, and if so, wouldn't getters and setters add to that?

7

The size of a struct refers to the struct's fields.

Automatic Properties include a field as part of their compiled structure; this field counts towards the size of the struct.

Otherwise, properties are no different from functions - they are not part of the struct's instance at all, since there's nothing particular to that instance in the property.

  • Thanks. I had a feeling it was just the fields. :) I'm confused on the last paragraph though. Could you go into more detail on that. Are you saying that once it's on the stack that it's really just the fields and their data, literally just the structured data, that's there and that the getters and setters will end up elsewhere? – Brian Chandler Mar 19 '15 at 16:56
  • @BrianChandler - yes. Functions of a struct don't get duplicated on the stack endlessly. – Telastyn Mar 19 '15 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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