-
Notifications
You must be signed in to change notification settings - Fork 6k
Add documentation for compiler error CS9036 #46258
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
717583e
Initial plan for issue
Copilot cea7032
Add documentation for compiler error CS9036
Copilot bb59f4a
Move CS9036 to compiler-messages folder and update TOC
Copilot 8bd831e
Add trailing newline to fix markdown lint issue
Copilot b7c7151
Remove duplicate file and update ms.date to 2025
Copilot 73416ff
Address final comments: add newline and remove from undocumented errors
Copilot 06cc04b
Update docs/csharp/language-reference/compiler-messages/cs9036.md
BillWagner 35442d6
Update cs9036.md
BillWagner 443f2a8
Fix markdown linting issues - remove trailing whitespace
Copilot d7429b9
Merge branch 'main' into copilot/fix-46104
gewarren 39e2afc
Merge branch 'main' into copilot/fix-46104
BillWagner ba4ed1e
Merge branch 'main' into copilot/fix-46104
BillWagner 9f4396a
Merge branch 'main' into copilot/fix-46104
BillWagner 15d4985
Add ai-usage metadata to CS9036 documentation
Copilot 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
67 changes: 67 additions & 0 deletions
67
docs/csharp/language-reference/compiler-messages/cs9036.md
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,67 @@ | ||
--- | ||
description: "Compiler Error CS9036" | ||
title: "Compiler Error CS9036" | ||
ms.date: 05/19/2025 | ||
ai-usage: ai-generated | ||
f1_keywords: | ||
- "CS9036" | ||
helpviewer_keywords: | ||
- "CS9036" | ||
--- | ||
# Compiler Error CS9036 | ||
|
||
Required member 'memberName' must be assigned a value, it cannot use a nested member or collection initializer. | ||
|
||
When initializing an object with a `required` member, you must directly assign the member a value. You cannot use a nested member or collection initializer to set properties of the `required` member without first instantiating it. | ||
|
||
## Example | ||
|
||
The following sample generates CS9036: | ||
|
||
```csharp | ||
class C | ||
{ | ||
public string? Prop { get; set; } | ||
} | ||
|
||
class Program | ||
{ | ||
public required C C { get; set; } | ||
|
||
static void Main() | ||
{ | ||
var program = new Program() | ||
{ | ||
// error CS9036: Required member 'Program.C' must be assigned a value, it cannot use a nested member or collection initializer. | ||
C = { Prop = "a" } | ||
}; | ||
} | ||
} | ||
``` | ||
|
||
## Solution | ||
|
||
To fix this error, directly assign a new instance of the required property and initialize its members: | ||
|
||
```csharp | ||
class C | ||
{ | ||
public string? Prop { get; set; } | ||
} | ||
|
||
class Program | ||
{ | ||
public required C C { get; set; } | ||
|
||
static void Main() | ||
{ | ||
var program = new Program() | ||
{ | ||
// Correct: Assign a new instance of C and then initialize its Prop property | ||
C = new C { Prop = "a" } | ||
}; | ||
} | ||
} | ||
``` | ||
|
||
For more information on required members, see the [required modifier](../keywords/required.md) reference article and [Object and Collection Initializers](../../programming-guide/classes-and-structs/object-and-collection-initializers.md) guide. |
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 |
---|---|---|
|
@@ -550,7 +550,6 @@ f1_keywords: | |
- "CS9033" | ||
- "CS9034" | ||
- "CS9035" | ||
- "CS9036" | ||
- "CS9037" | ||
- "CS9038" | ||
- "CS9039" | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.