A value object is a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object.
- They have no identity
- They are immutable
Immutability is an important requirement. The values of a value object must be immutable once the object is created. Therefore, when the object is constructed, you must provide the required values, but you must not allow them to change during the object's lifetime.
public record Phone
{
// init makes is immutable
public string Number { get; init; }
public string Type { get; init; }
public Phone(string Number, string Type)
{
this.Number = Number;
this.Type = Type;
}
}
...
// Using public auto-implemented properties
public record Phone(string Number, string Type)