« Record » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:CSharp = Links = * [https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record] ») |
Aucun résumé des modifications |
||
Ligne 1 : | Ligne 1 : | ||
[[Category:CSharp]] | [[Category:CSharp]] | ||
= Links = | |||
* [https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record Record] | |||
= Definition = | |||
Define a reference type. C# 10 allows the {{boxx|record class}} syntax as a synonym to clarify a reference type, and {{boxx|record struct}} to define a value type. | |||
= Primary constructor = | |||
When you declare a primary constructor on a record, the compiler generates public properties for the primary constructor parameters. | |||
<kode lang='cs'> | |||
public record Person(string FirstName, string LastName); | |||
// with the generated properties it is equivalent to | |||
public record Person | |||
{ | |||
public required string FirstName { get; init; } | |||
public required string LastName { get; init; } | |||
}; | |||
</kode> |
Version du 9 août 2024 à 13:18
Links
Definition
Define a reference type. C# 10 allows the record class syntax as a synonym to clarify a reference type, and record struct to define a value type.
Primary constructor
When you declare a primary constructor on a record, the compiler generates public properties for the primary constructor parameters.
public record Person(string FirstName, string LastName); // with the generated properties it is equivalent to public record Person { public required string FirstName { get; init; } public required string LastName { get; init; } }; |