As I know record type can be immutable(cannot be changed once object is created) reference type.
So what are different prectical scenarios where record type uses is more relevant and appropriate instead of Class?
As I know record type can be immutable(cannot be changed once object is created) reference type.
So what are different prectical scenarios where record type uses is more relevant and appropriate instead of Class?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Saravanan GanesanPosted Sep 25, 2023, 5:43 PM
Record types are ideal for representing immutable data with value semantics, such as entities with no identity change, like date/time objects, configurations, or DTOs. They simplify code, enhance readability, and reduce the chance of bugs in scenarios where data changes should not occur, making them more appropriate than classes for such cases.
Munib ButtPosted Jan 12, 2023, 8:30 PM
Use Records in C# when you want to compare instances using values of the properties.
Muhammad Imran AnsariPosted Jan 12, 2023, 6:44 PM
In a simple way, a class is an OOP concept that wraps data with functionality, while a record represents a set of data.
Records should not have any state changes after instantiation (immutable state), while classes change properties.
Use a record when an object's only purpose is to contain public data. On other hand, use a class if your object has unique logic. Classes are mutable so even if they have the same data, doesn't mean they are the same.
Rajanikant HawaldarPosted Jan 12, 2023, 4:44 PM
If main purpose of storing data then use record types, if responsibility need to define then use class
Aravind GovindarajPosted Jan 12, 2023, 4:00 PM
Hi, Please refer to this link it clears your queries
https://josipmisko.com/posts/c-sharp-class-vs-record