Alex Rivera | Logout

Why is struct better with being less than 16 bytes

Asked 2010-03-13T10:01:16.530
17

I'm studying .NET structs - some books advise creating structs if they have an instance size less than 16 bytes.

Why is that?

Edit
Report

1 Answer

2

It's not exactly 16 bytes, but because structs are passed by value (copied whenever you give them to a method, assign them, etc.) they must not be too large. If they are, passing stuff by reference is cheaper.

4 years later: I answered this knowing very little about .NET (still don't know much about it). Guffa's answer is obviously more correct when dealing with "more or less than 16 bytes?", since at that size a bit of copying shouldn't matter much. My answer may be something to keep in mind when building huge structs.

answered 2010-03-13T10:03:56.360

Your Answer