Skip to content

Add documentation for object initializer syntax without 'new' keyword #47036

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 30, 2025

This PR addresses the missing documentation for the C# object initializer syntax that lacks the new keyword, as specified in C# ECMA spec 12.7.11.3. This feature allows initializing nested objects using = { } syntax without creating new instances.

Changes Made

Enhanced Documentation

  • object-and-collection-initializers.md: Added comprehensive section explaining the difference between Property = { ... } and Property = new() { ... } syntaxes
  • how-to-initialize-objects-by-using-an-object-initializer.md: Added brief introduction to the syntax without new keyword

New Code Examples

  • ObjectInitializerWithoutNew.cs: Created focused examples demonstrating:
    • Difference between modifying existing instances vs. creating new ones
    • Practical use cases with read-only properties
    • Working code that compiles and runs successfully

Key Feature Documented

The ability to initialize objects deeply within properties using = { } syntax without the new keyword:

// Modifies existing instance (no 'new' keyword)
var person = new Person
{
    Name = "Alice",
    Address = { Street = "123 Main St", City = "Anytown" }
};

// Works with read-only properties
var app = new Application
{
    Name = "MyApp",
    AppSettings = { Theme = "Dark", FontSize = 14 }  // AppSettings is read-only
};

This syntax is particularly valuable when:

  • Working with read-only properties that don't have setters
  • Wanting to modify existing instances rather than replace them
  • Initializing nested properties deeply within object hierarchies

All code examples have been tested and compile successfully with .NET 8.

Fixes #12979.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/csharp/programming-guide/classes-and-structs/how-to-initialize-objects-by-using-an-object-initializer.md docs/csharp/programming-guide/classes-and-structs/how-to-initialize-objects-by-using-an-object-initializer
docs/csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md docs/csharp/programming-guide/classes-and-structs/object-and-collection-initializers

@Copilot Copilot AI changed the title [WIP] Missing docs and example on object initializer that lacks a new keyword Add documentation for object initializer syntax without 'new' keyword Jun 30, 2025
@Copilot Copilot AI requested a review from BillWagner June 30, 2025 19:52
Copilot finished work on behalf of BillWagner June 30, 2025 19:52
Copy link
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.

One major change, and this is ready to go.

@Copilot Copilot AI requested a review from BillWagner July 1, 2025 17:56
Copilot finished work on behalf of BillWagner July 1, 2025 17:56
Copy link
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.

Thanks @copilot

This is now ready for final review.

@BillWagner BillWagner marked this pull request as ready for review July 1, 2025 18:27
@BillWagner BillWagner requested a review from a team as a code owner July 1, 2025 18:27
@BillWagner BillWagner requested a review from IEvangelist July 1, 2025 18:27
@BillWagner
Copy link
Member

ping @IEvangelist for review

@BillWagner BillWagner enabled auto-merge (squash) July 1, 2025 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing docs and example on object initializer that lacks a new keyword
2 participants