DEV Community

PeterMilovcik
PeterMilovcik

Posted on

When to use record instead of class in .NET?

In .NET, the record keyword is used to create a value type that is a light-weight alternative to a class. record types are similar to classes, but they are immutable and come with built-in support for value-based equality, copy and move semantics, and pattern matching. record types are particularly useful for scenarios where you need to represent a value that is not intended to be modified, such as a mathematical vector or a geographic coordinate.

In general, you should use record instead of class when you need a value type that is lightweight, immutable, and provides built-in support for value-based equality, copying, and pattern matching. If you need a reference type that is mutable or you require additional features that are not supported by record types, such as inheritance or polymorphism, then you should use a class instead.

Latest comments (0)