In some code base I am working in, initially a simple type was implemented similarly to this (I can't post exact code but to the purpose of this question, the shown code is perfectly equivalent):
class S {
public String Name { get; }
public S() : this(null) { }
public S(string name) { Name = name ?? ""; }
public Blah Frob() { .... }
}
Later, there was an undocumented change where the type was changed to a value type:
struct S {
private String name;
public String Name {
get => name ?? "";
private set => name = value; }
public S(string name) { this.name = name; }
public Blah Frob() { .... }
}
I know perfectly well the diferences between value types and reference types but I'm not really sure what could be the reason that made this change seem the right thing to do. What are the benefits of an immutable value type that holds only a reference to another immutable reference type when compared to the equivalent refence type? What am I missing?
I seem to recall Eric Lippert commenting once to a question as to why string
wasn't a struct along the same lines; what was the benefit of a struct that held only one reference to a reference type, but I can't find it anymore.