Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a (proposed) “Record” class in C# 6?

Tags:

c#

c#-6.0

I believe this never made it into C# 6k, but may be considered for a later release.

I have just read a very confusing blog talking about pattern matching and record classes in C# 6 along with the operator “is”.

Please can someone give me a overview of what all this is about in such a way as I can understand what effect it will have on C# code I write once C# 6 ships.

(Please note this has nothing to do with databases! But Google thinks it does...)


I read Pattern Matching in C# 6 and VB 12 first before finding Easier Immutable Objects in C# 6 and VB 12 - reading them in the other order makes a lot more sence.

If Record classes solve the:

Currently most ORMs and serializers don’t have support for immutable types. Instead, they assume there will be a parameterless constructor and mutable properties.

Problem by standardising how immutable types are created, then I can see them being great regardless of pattern matching.

like image 640
Ian Ringrose Avatar asked Aug 20 '14 09:08

Ian Ringrose


People also ask

What is a record type in C?

A record is a reference type and follows value-based equality semantics. You can define a record struct to create a record that is a value type. To enforce value semantics, the compiler generates several methods for your record type (both for record class types and record struct types):

What is a record class?

A record class declares a sequence of fields, and then the appropriate accessors, constructors, equals , hashCode , and toString methods are created automatically. The fields are final because the class is intended to serve as a simple "data carrier".

What is record class C#?

C# 10 allows the record class syntax as a synonym to clarify a reference type, and record struct to define a value type with similar functionality. You can create record types with immutable properties by using positional parameters or standard property syntax.

What is the difference between a record and a class?

Class vs Record Records are immutable, while classes are not. Other differences include between class and record type include: We define records using the record keyword instead of the class keyword. Records should not have any state changes after instantiation, while classes change properties.


2 Answers

From here:

This is essentially an immutable class defined solely by its constructor. Here is an example from the specification:

public record class Cartesian(double x: X, double y: Y);

In addition to the constructor, the compiler will automatically create:

  • A read-only property for each parameter
  • An Equals function
  • A GetHashCode override
  • A ToString Override
  • An “is” Operator, known as “Matches” in VB

I just read this PROPOSAL: Records, and Plain Old CLR Objects

Imagine something called a "Record", a type with an ordered list of named data members. Not saying there should be a new kind of type called a "record type" alongside classes and structs... indeed it might be best not to have a new kind of type, since we might want record-like classes and structs.

PROPOSAL 1: a record can be defined using primary-constructor syntax, and it is syntactic sugar for an expanded form as follows...

class Point(int X, int Y);

==>

class Point(int X, int Y)
{
   public int X { get; } = X;
   public int Y { get; } = Y;
}

The rule is: "When you write a record, it automatically generates properties for the PRIMARY PROPERTIES, unless you have provided those properties yourself". The term "Primary Properties" refers to the parameters in the primary-constructor syntax. Thus, if you didn't want it to auto-generate a property, you'd have to provide your own version of that property, as in the example below. (There's no way to say that you don't want the property with this syntax: if you don't want the property, then don't use the feature at all).

class Point(int X, int Y) { public int X => 15; }

==>

class Point(int X, int Y)
{
   public int X => 15;
   public int Y {get; set;} = Y;
}
like image 82
Rahul Tripathi Avatar answered Sep 23 '22 19:09

Rahul Tripathi


According to this discussion, it's a way for the compiler generate immutable types (class or struct) ready for pattern matching and with record semantics.

The compiler will generate an is operator for pattern matching and Equals, GetHashCode and ToString methods with record semantics.

like image 21
Paulo Morgado Avatar answered Sep 23 '22 19:09

Paulo Morgado