From the official docs:
AVOID defining a struct unless the type has all of the following characteristics:
[…]
It has an instance size under 16 bytes.
[…]
I know that one reason for keeping it under 16 bytes is that structs are intended to be used on the stack. However, are there other reasons?
In order to not accidentally create an XY problem, what I want to know is:
Does this 16 byte rule still make sense in a situation where I use a struct as a private field in a class?
We all love code, so here’s an example:
public struct Address { public readonly string Line1; public readonly string Line2; public Address(string line1, string line2) { Line1 = line1; Line2 = line2; } } public class Person { private Address _address; [...] public void ChangeAddressLine1(string newLine1) { _address = new Address(newLine1, _address.Line2); } }