-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Document init accessor #23082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Document init accessor #23082
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| --- | ||
| description: "init keyword - C# Reference" | ||
| title: "init keyword - C# Reference" | ||
| ms.date: 03/03/2021 | ||
| f1_keywords: | ||
| - "init" | ||
| - "init_CSharpKeyword" | ||
| helpviewer_keywords: | ||
| - "init keyword [C#]" | ||
| --- | ||
| # init (C# Reference) | ||
|
|
||
| In C# 9 and later, the `init` keyword defines an *accessor* method in a property or indexer. An init-only setter assigns a value to the property or the indexer element only during object construction. For more information and examples, see [Properties](../../programming-guide/classes-and-structs/properties.md), [Auto-Implemented Properties](../../programming-guide/classes-and-structs/auto-implemented-properties.md), and [Indexers](../../programming-guide/indexers/index.md). | ||
|
|
||
| The following example defines both a `get` and an `init` accessor for a property named `Seconds`. It uses a private field named `_seconds` to back the property value. | ||
|
|
||
| [!code-csharp[init#1](snippets/InitExample1.cs)] | ||
|
|
||
| Often, the `init` accessor consists of a single statement that assigns a value, as it did in the previous example. You can implement the `init` accessor as an expression-bodied member. The following example implements both the `get` and the `init` accessors as expression-bodied members. | ||
|
|
||
| [!code-csharp[init#3](snippets/InitExample3.cs)] | ||
|
|
||
| For simple cases in which a property's `get` and `init` accessors perform no other operation than setting or retrieving a value in a private backing field, you can take advantage of the C# compiler's support for auto-implemented properties. The following example implements `Hours` as an auto-implemented property. | ||
|
|
||
| [!code-csharp[init#2](snippets/InitExample2.cs)] | ||
|
|
||
| ## C# language specification | ||
|
|
||
| [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] | ||
|
|
||
| ## See also | ||
|
|
||
| - [C# Reference](../index.md) | ||
| - [C# Programming Guide](../../programming-guide/index.md) | ||
| - [C# Keywords](index.md) | ||
| - [Properties](../../programming-guide/classes-and-structs/properties.md) | ||
10 changes: 10 additions & 0 deletions
10
docs/csharp/language-reference/keywords/snippets/InitExample1.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class InitExample | ||
| { | ||
| private double _seconds; | ||
|
|
||
| public double Seconds | ||
| { | ||
| get { return _seconds; } | ||
| init { _seconds = value; } | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
docs/csharp/language-reference/keywords/snippets/InitExample2.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| class InitExampleAutoProperty | ||
| { | ||
| public double Hours { get; init; } | ||
| } |
10 changes: 10 additions & 0 deletions
10
docs/csharp/language-reference/keywords/snippets/InitExample3.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class InitExampleExpressionBodied | ||
| { | ||
| private double _seconds; | ||
|
|
||
| public double Seconds | ||
| { | ||
| get => _seconds; | ||
| init => _seconds = value; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidwengier Do we need this? or only init_CSharpKeyword is sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly don't need it from Roslyn's point of view.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BillWagner The articles in the keywords folder have two entries under
f1_keywords(sometimes in reverse order):Is the second entry superfluous?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the second entry is superfluous. Ping @ghogen to verify.