Skip to content

Commit d029275

Browse files
CopilotBillWagnergewarrenadegeo
authored
Add documentation for compiler error CS9036 (#46258)
* Initial plan for issue * Add documentation for compiler error CS9036 Co-authored-by: BillWagner <[email protected]> * Move CS9036 to compiler-messages folder and update TOC Co-authored-by: BillWagner <[email protected]> * Add trailing newline to fix markdown lint issue Co-authored-by: BillWagner <[email protected]> * Remove duplicate file and update ms.date to 2025 Co-authored-by: BillWagner <[email protected]> * Address final comments: add newline and remove from undocumented errors Co-authored-by: BillWagner <[email protected]> * Update docs/csharp/language-reference/compiler-messages/cs9036.md * Update cs9036.md * Fix markdown linting issues - remove trailing whitespace Co-authored-by: BillWagner <[email protected]> * Add ai-usage metadata to CS9036 documentation Co-authored-by: adegeo <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]> Co-authored-by: adegeo <[email protected]>
1 parent 0857f16 commit d029275

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
description: "Compiler Error CS9036"
3+
title: "Compiler Error CS9036"
4+
ms.date: 05/19/2025
5+
ai-usage: ai-generated
6+
f1_keywords:
7+
- "CS9036"
8+
helpviewer_keywords:
9+
- "CS9036"
10+
---
11+
# Compiler Error CS9036
12+
13+
Required member 'memberName' must be assigned a value, it cannot use a nested member or collection initializer.
14+
15+
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.
16+
17+
## Example
18+
19+
The following sample generates CS9036:
20+
21+
```csharp
22+
class C
23+
{
24+
public string? Prop { get; set; }
25+
}
26+
27+
class Program
28+
{
29+
public required C C { get; set; }
30+
31+
static void Main()
32+
{
33+
var program = new Program()
34+
{
35+
// error CS9036: Required member 'Program.C' must be assigned a value, it cannot use a nested member or collection initializer.
36+
C = { Prop = "a" }
37+
};
38+
}
39+
}
40+
```
41+
42+
## Solution
43+
44+
To fix this error, directly assign a new instance of the required property and initialize its members:
45+
46+
```csharp
47+
class C
48+
{
49+
public string? Prop { get; set; }
50+
}
51+
52+
class Program
53+
{
54+
public required C C { get; set; }
55+
56+
static void Main()
57+
{
58+
var program = new Program()
59+
{
60+
// Correct: Assign a new instance of C and then initialize its Prop property
61+
C = new C { Prop = "a" }
62+
};
63+
}
64+
}
65+
```
66+
67+
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.

docs/csharp/language-reference/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,8 @@ items:
19841984
href: ./compiler-messages/cs8812.md
19851985
- name: CS8515
19861986
href: ./compiler-messages/cs8515.md
1987+
- name: CS9036
1988+
href: ./compiler-messages/cs9036.md
19871989
- name: CS9043
19881990
href: ./compiler-messages/cs9043.md
19891991
- name: Level 1 warning messages

docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,6 @@ f1_keywords:
550550
- "CS9033"
551551
- "CS9034"
552552
- "CS9035"
553-
- "CS9036"
554553
- "CS9037"
555554
- "CS9038"
556555
- "CS9039"

0 commit comments

Comments
 (0)