If a structure is going to have, and its users will expect it to have, all three of the following characteristics:
- Its entire state is encapsulated in some particular fixed set of readable members
- An instance may be readily created in which those members have any combination of values which are valid for their respective types.
- A default instance of the type should have all of those members set to the default values of their respective types.
the type should expose its members as fields. The above requirements mean a struct won't be able to do anything an exposed-field struct can't do, and will also mean that if code doesn't hit any threading hazards the struct will be able to do anything an exposed-field struct can do, albeit perhaps more slowly and with more threading hazards.
If a struct Foo
has fields f1
and f2
, and a constructor which sets those fields in that order, and if fooExpr
is some sort of expression of type Foo
[perhaps a variable, field, array reference, property, or whatever] the statement:
myFoo.f2 = someValue;
will only be allowed in cases where one could legally say
myFoo = new Foo(myFoo.f1, someValue);
and in all circumstances where the first form is allowed and the behavior of the second form has defined semantics, they will behave identically. Thus, trying to "encapsulate" the properties of Foo
doesn't really accomplish anything except make the code more cumbersome to write, less clear, and slower to execute.