C# Difference between Record and class

T.Zacks 3,996 Reputation points
2022-09-12T18:09:41.14+00:00

I read this article https://josipmisko.com/posts/c-sharp-class-vs-record

They said
The main difference between class and record type in C# is that a record has the main purpose of storing data, while class define responsibility. Records are immutable, while classes are not.

This is not clear to me that Records are immutable what does it mean immutable?

please share a little sample code in c# which prove that Records are immutable and classes are not. Thanks

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2022-09-12T18:33:37.34+00:00

    It's complicated as to what records are. They are a new addition. The gist of the matter is that records are designed for the common case of "data only" types. They more closely resemble structs than classes. A class is an OOP concept that wraps data with functionality. A record is just a set of data.

       record Point  
       {  
           public int X { get; init; }  
           public int Y { get; init; }  
       }  
         
       class Rectangle  
       {  
          public Point TopLeft { get; set; }  
          public Point BottomRight { get; set; }  
       }  
    

    Immutability isn't something that you "prove". It is enforced by the language. But note that records do not have to be immutable (just like structs and classes don't have to be). But they are primarily designed for immutability purposes (hence just a set of related data). Examples might include points, coordinates or the parts of a complex number. By themselves the data points mean nothing, but combined together you have something useful. That is what a record represents.

       var topLeft = new Point() { X = 10, Y = 20 };  
       topLeft.X = 20;  //Compiler error, X cannot be written  
         
       var rect = new Rectangle() { TopLeft = topLeft, BottomRight = new Point() { X = 20, Y = 40 } };  
       rect.TopLeft = new Point() { X = 50, Y = 50 };  //Writable  
    

    To be clear classes can be immutable as well (e.g. strings). But normally classes allow modifications because they are representing an OOP entity. Records are just for data storage and have no functionality. Making them immutable is easy to do with a struct or class. But the new record type makes it more clear what the purpose of the type is and makes it harder to get it wrong. Comparing records to structs/classes might lead you to believe they are the same and they mostly are. However classes are reference types (google that) and follow reference equality. You cannot change that. Records use value equality which makes them act more like structs.

    Honestly, if you don't understand structs and classes in C# yet then you are too new at the language to worry about records. Records are an advanced concept, in my opinion. You'll know when you need them. You will likely not run into that situation unless you're dealing with highly performant code. Most types will remain classes.

    Refer to the official documentation for more information as this is a complex topic. The docs also provide more than enough examples on all this stuff.

    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record
    https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/records

    https://www.c-sharpcorner.com/article/c-sharp-9-0-introduction-to-record-types/
    https://www.davidhayden.me/blog/csharp-9-record-type

    24 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.