DEV Community

PeterMilovcik
PeterMilovcik

Posted on

1

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.

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay