Skip to content

C# 10 reference update: with expression and structure types#26100

Merged
BillWagner merged 2 commits intodotnet:mainfrom
pkulikov:struct-with
Sep 15, 2021
Merged

C# 10 reference update: with expression and structure types#26100
BillWagner merged 2 commits intodotnet:mainfrom
pkulikov:struct-with

Conversation

@pkulikov
Copy link
Copy Markdown
Contributor

No description provided.

Any record type has the *copy constructor*. That is a constructor with a single parameter of the containing record type. It copies the state of its argument to a new record instance. At evaluation of a `with` expression, the copy constructor gets called to instantiate a new record instance based on an original record. After that, the new instance gets updated according to the specified modifications. By default, the copy constructor is implicit, that is, compiler-generated. If you need to customize the record copy semantics, explicitly declare a copy constructor with the desired behavior. The following example updates the preceding example with an explicit copy constructor. The new copy behavior is to copy list items instead of a list reference when a record is copied:
## Custom copy semantics

Any record class type has the *copy constructor*. That is a constructor with a single parameter of the containing record type. It copies the state of its argument to a new record instance. At evaluation of a `with` expression, the copy constructor gets called to instantiate a new record instance based on an original record. After that, the new instance gets updated according to the specified modifications. By default, the copy constructor is implicit, that is, compiler-generated. If you need to customize the record copy semantics, explicitly declare a copy constructor with the desired behavior. The following example updates the preceding example with an explicit copy constructor. The new copy behavior is to copy list items instead of a list reference when a record is copied:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry: the added header doesn't show the convenient diff. The only change in this paragraph is at the very first sentence. Please take a note.

Copy link
Copy Markdown
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great @pkulikov

I'll :shipit: now.

@BillWagner BillWagner merged commit a4f3ce8 into dotnet:main Sep 15, 2021
@pkulikov pkulikov deleted the struct-with branch September 15, 2021 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants