Bringing F# records to C#

Bringing F# records to C#

Alex Netkachov

Record is a programming language construction. Very roughly, it is an immutable class without methods with struct-like equality (i.e. two records are equal when all their fields are equal). This makes them ideal for representing value objects in DDD.

let tom = { FirstName = "Tom", LastName = "Johns" }

Record is immutable, which is good for multi-threading. Changing property of the record creates new record.

let tim = { person with FirstName = "Tim" }

One who likes records may want to use them in C# but they do not exist here. However, it is not too complicated to design class that works similar to record.

And then use it:

Immutable? Yes. Extendable? Yes. Cool? Yes.

Play with it on https://dotnetfiddle.net/neyDjv

Follow me on Twitter or Telegram.


Report Page