DEV Community

Discussion on: C# 9.0 Optional Property in Record

Collapse
peledzohar profile image
Zohar Peled

Side note: You should probably not use setters in record type properties - as being immutable is kind of the main point of record in the first place.

C# 9.0 introduces record types, which are a reference type that provides synthesized methods to provide value semantics for equality. Records are immutable by default.

You should probably change

record Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

to

record Employee
{
    public string FirstName { get; init; }
    public string LastName { get; init; }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
wrijugh profile image
Wriju's Blog Author

I have addressed this already in my previous blog dev.to/wrijugh/c-9-0-init-only-pro...

Collapse
peledzohar profile image
Zohar Peled

Yeah, I've seen your post about init only properties, but that's not the point of my comment - the point is that (IMHO) records shouldn't have setters on their properties.