Working with fields which can mutate or be new instances altogether

Posted by dotnetdev on Stack Overflow See other posts from Stack Overflow or by dotnetdev
Published on 2010-03-29T12:47:13Z Indexed on 2010/03/29 13:03 UTC
Read the original article Hit count: 203

Filed under:
|

Structs are usually used for immutable data, eg a phone number, which does not mutate, but instead you get a new one (eg the number 000 becoming 0001 would mean two seperate numbers).

However, pieces of information like Name, a string, can either mutate (company abc changing its name to abcdef, or being given a new name like def). For fields like this, I assume they should reside in the mutable class and not an immutable structure?

My way of structuring code is to have an immutable concept, like Address (any change is a new address completely), in a struct and then reference it from a class like Customer, since Customer always has an address. So I would put CompanyName, or Employer, in the class as it is mutable. But a name can either mutate and so be the same 1 instance, or a new name setup and while the company still owning the first name too.

Would the correct pattern for assigning a new instance (eg a new company name but the old name still owned by the company) be?:

string name = "";
string newName = new string();
newName = "new";
name = newName;

And a mutation just the standard assignment pattern?

Thanks

© Stack Overflow or respective owner

Related posts about struct

Related posts about c#