I was taught that structs should almost always be immutable, so this unusual behaviour of record classes vs. record structs threw me off guard.
Using a record class...
record class Person(string FirstName, string LastName);
Person p = new("John", "Smith");
p.FirstName = "Jack" // Not allowed!
Using a record struct...
record struct Person(string FirstName, string LastName);
Person p = new("John", "Smith");
p.FirstName = "Jack" // Fine!
Using a readonly record struct...
readonly record struct Person(string FirstName, string LastName);
Person p = new("John", "Smith");
p.FirstName = "Jack" // Now allowed!
Why are non readonly record structs mutable by default, and why doesn't the same behaviour apply for record classes?
Edit: I guess what I'm asking here is, why is the syntax... weird?
For example, it would seem more logical like:
record class- mutable reference type with value semantics.readonly record class- immutable reference type with value semantics.record struct- mutable value type with value semantics.readonly record struct- immutable value type with value semantics.