Skip to content

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 14 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions docs/csharp/language-reference/compiler-messages/cs9036.md
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.
2 changes: 2 additions & 0 deletions docs/csharp/language-reference/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1984,6 +1984,8 @@ items:
href: ./compiler-messages/cs8812.md
- name: CS8515
href: ./compiler-messages/cs8515.md
- name: CS9036
href: ./compiler-messages/cs9036.md
- name: CS9043
href: ./compiler-messages/cs9043.md
- name: Level 1 warning messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,6 @@ f1_keywords:
- "CS9033"
- "CS9034"
- "CS9035"
- "CS9036"
- "CS9037"
- "CS9038"
- "CS9039"
Expand Down