The vast majority (99.99%) of structures written for use in .net (whether defined in the Framework or in user code) either represent a fixed collection of independent values, which can accept any combination of values that are legal for their types (e.g. a Point
, which contains an X
and a Y
), or else represent a single abstract entity. Microsoft's structure usage guidelines in .net fail to distinguish between these different usage scenarios, assuming all structures fit the second pattern, with the unfortunate consequence that structures of the first type are often written in a fashion which is only really appropriate for those of the second. As it happens, though, the DateTime
structure in .net certainly does not qualify as a collection of independent values, and thus the guidelines are entirely appropriate for it.
First of all, there's not really any good way that a DateTime
could directly expose any fields. There's no clear set of independent values that a DateTime
structure encapsulates, so it's unclear what the types of any fields should be. Further, many of the properties cannot sensibly be mutated because they're not independent; for example, if one has a DateTime
which represents February 29, 2012, and one sets the year to 2011 and then sets the month to 3, what should the resulting date be? It only makes sense to mutate a DateTime
using methods, and unfortunately both vb.net and C# will pretend to allow mutating methods to be called on a read-only structure instance, by silently replacing code like MyList[3].MutatingMethod();
with the horribly-broken var temp=MyList[3]; temp.MutatingMethod();
. It should be fairly simple for Microsoft to fix this problem by defining an attribute such that invoking on a read-only instance a method tagged with the attribute should result in a compiler error rather than broken code, but unfortunately Microsoft's language people would rather declare that mutable structures are evil, than fix the language so they wouldn't be,