dotnet / dotnet/docs

The examples should use types that exhibit value equality

Open
#37,262 1 comment 0 reactions 0 assignees View on GitHub
dotnet-csharp/svc fundamentals/subsvc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

### Type of issue

Missing information

### Description

The article mentions value equality as a major factor in the decision to use records over non-record classes and structs.

However, the examples contradict this by using arrays, which have reference equality and not value equality. The most clear example of this is the example immediately below "The following example demonstrates value equality in records:" where the signature takes an array (granted, it also takes two `string`s, which are also reference types, but at least they _exhibit_ value-type behaviour).

Also, I feel that there's slight ambiguity in the 'When to use records' section. Should the two bullet points be an 'or' list, or an 'and' list, i.e. should I use records if I just want immutability and don't care about value equality, or should **both** points be a factor. I believe that both points should be a factor.

Perhaps something to consider in the near future is to add a section comparing records to primary constructors on classes in C# 12. One of my current areas of contention at work is members of the team using records purely for the conciseness of the primary constructors, but where their actual type is neither immutable or exhibits value-equality. Hopefully that contention will go in C# 12, but I think guidance on records vs. classes-with-primary-constructors would be a good addition.

Many thanks - keep up the great work!

### Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/records

### Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/fundamentals/types/records.md

### Document Version Independent Id

8991024e-b64a-bc2a-9612-5649d89bda2b

### Article author

BillWagner

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.