-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
Type of issue
Missing information
Description
I am looking up the specific differences between record, class, struct and all of their possible combinations
Records are types that use value-based equality. You can define records as reference types or value types. Two variables of a record type are equal if the record type definitions are identical, and if for every field, the values in both records are equal. Two variables of a class type are equal if the objects referred to are the same class type and the variables refer to the same object. Value-based equality implies other capabilities you probably want in record types. The compiler generates many of those members when you declare a record instead of a class. The compiler generates those same methods for record struct types.
Possible layout:
| Type | Comparison as... | Equality by... | Immutability | Inheritance | .ToString() Output |
|---|---|---|---|---|---|
record |
Referencetype | Value | ✅ | only from other records | MyRecord { SomeProperty = "Hello World" , SomeOtherProperty = "Nice to see you!" } |
record struct |
Value Type | Value | ✅ (?) | ❌ (?) | ? |
record class |
Reference Type | ? | ? | ??? | ?? |
class |
Reference Type | Reference | ❌ | ✅ | MyClassType (no Property values) |
(to be continued if you know more possible combinations 👍 )
(understand all columns as default "without override"!)
additional to the sense making Overview Table of differences across all the combinations (feel free to create a new docs enhancement request if you need to from this 👍 )
Improvable Tutorial parts for records
https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/records
-
The first sentence in the Tutorial speaks about
value-based equality, while the next sentence lets me know thatYou can define records as reference types or value types.Records are types that use value-based equality. You can define records as reference types or value types.
-
But only the linked Build-in Types Reference for Records (C# Reference) states:
The record modifier provides built-in functionality for encapsulating data. The record class and record syntax define reference types. The record struct syntax defines a value type.
-
To me the unclear or optimizeable parts in the Tutorial Article, which should assume the Reader is new to Records and might even still be learning C#'s basics, this Tutorial misses some important base-laying information to then have the suitable and expectable knowledge instead of making assumtions about his knowledge state, which the Consumer might not be aware/told of::
- eventual Prerequired/Expected Knowledge
- What are the defaults for each of the listed variants we are told about in this section (!) up from the start of the Tutorial *(Start each from the simple
recorddefinition, do not group them!) use the sampled table for the Overview to get an idea what would be interesting to know)
-
Code Samples: The used Code Samples in this tutorial could benefit brevity by some common code styling:
- Add empty new lines and
- Use Line-Breaks for Code Sample using Linq Extensions and the Non destructive
withsample
-
At the End of the Tutorial, you included some links, but beneath the net9.0 proposal, we seem to miss a link to the latest (net10 like for the last link for
record struct?) .NET version maybe?You can learn more about records in the C# language reference article for the record type and the proposed record type specification and record struct specification.
You can learn more about records in the [C# language reference article for the record type](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record) and the [proposed record type specification](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/records) and [record struct specification](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/record-structs).
Page URL
https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/
Content source URL
https://github.com/dotnet/docs/blob/main/docs/csharp/fundamentals/types/index.md
Document Version Independent Id
9aa4f01b-a5a0-3cb1-3c22-db333c0d9f11
Platform Id
30e147af-ed76-bf1c-6227-a0627346a70b
Article author
Metadata
- ID: 1991c89a-9728-ea31-a999-79b3cb331372
- PlatformId: 30e147af-ed76-bf1c-6227-a0627346a70b
- Service: dotnet-csharp
- Sub-service: fundamentals