Usally I choose between struct and class not because of memory issues but because of semantics of the type. Some of my value types have quite large memory footprint, sometimes too large to copy this data all the time. So I wonder if it is a good idea to pass immutable value objects always by reference? Since the objects are immutable they cannot by modified by methods that accept them by reference. Are there other issues when passing by reference?
Usally I choose between struct and class not because of memory issues but because
Share
That suggests they shouldn’t be value types, from an implementation point of view. From "Design Guidelines for Developing Class Libraries", section "Choosing Between Classes And Structures":
It sounds like you should be creating immutable reference types instead. In many ways they end up "feeling" like value objects anyway (think strings) but you won’t need to worry about the efficiency of passing them around.
"Immutability" for value types is a slightly fluid concept – and it certainly doesn’t mean that using
refis safe:We’re not changing one part of the value – we’re replacing the whole of the value of
xwith an entirely different value.Immutability is somewhat easier to think about when it comes to reference types – although even then you can have an object which in itself won’t change, but can refer to mutable objects…