Skip to content

Document projection initializers (simplified names) for anonymous types #47709

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 4 commits into from
Aug 4, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 1, 2025

Added comprehensive documentation for anonymous type projection initializers, which allows developers to use simplified syntax when creating anonymous types. This feature lets you use local variables directly without explicitly specifying member names.

Changes Made

Documentation Enhancement

  • Added a new "Projection initializers" section to the anonymous types documentation
  • Provided clear explanations of when member names are inferred vs. when they must be explicit
  • Added guidance on limitations and edge cases

Code Examples

Added practical examples demonstrating the simplified syntax:

// Explicit member names
var personExplicit = new { FirstName = "Kyle", LastName = "Mit" };

// Projection initializers (inferred member names)
var firstName = "Kyle";
var lastName = "Mit";
var personInferred = new { firstName, lastName };

The documentation now clearly shows that these two approaches create equivalent anonymous types, addressing the original request for side-by-side comparisons.

Additional Context

  • Explains when member name inference fails (duplicate names, reserved names, invalid identifiers)
  • References the existing IDE0037 style rule for enforcing consistent usage
  • Uses terminology consistent with tuple projection initializers documentation

All code examples have been tested and verified to compile and run correctly. The documentation follows Microsoft Writing Style Guide conventions and includes proper cross-references.

Fixes #33322.


💡 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/fundamentals/types/anonymous-types.md docs/csharp/fundamentals/types/anonymous-types

@dotnet-policy-service dotnet-policy-service bot added the okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. label Aug 1, 2025
Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Document Simplified Names on Anonymous Types page Document projection initializers (simplified names) for anonymous types Aug 1, 2025
@Copilot Copilot AI requested a review from BillWagner August 1, 2025 20:05
Copilot finished work on behalf of BillWagner August 1, 2025 20:05
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.

This is a great start. One sentence is too vague.

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI requested a review from BillWagner August 4, 2025 16:32
Copilot finished work on behalf of BillWagner August 4, 2025 16:32
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.

This is now ready for final review.

@BillWagner BillWagner marked this pull request as ready for review August 4, 2025 17:36
@BillWagner BillWagner requested a review from a team as a code owner August 4, 2025 17:36
@BillWagner BillWagner requested a review from tdykstra August 4, 2025 17:36
Copy link
Contributor

@gewarren gewarren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add periods in code comments.

@BillWagner
Copy link
Member

Please add periods in code comments.

@copilot Please make this change.

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Aug 4, 2025

Please add periods in code comments.

@copilot Please make this change.

Added periods to all code comments in the anonymous types examples as requested. (535b15b)

@Copilot Copilot AI requested a review from BillWagner August 4, 2025 18:03
Copilot finished work on behalf of BillWagner August 4, 2025 18:03
@BillWagner BillWagner enabled auto-merge (squash) August 4, 2025 18:13
@BillWagner BillWagner merged commit 1e06323 into main Aug 4, 2025
10 checks passed
@BillWagner BillWagner deleted the copilot/fix-33322-2 branch August 4, 2025 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-csharp/svc fundamentals/subsvc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document Simplified Names on Anonymous Types page
3 participants