Skip to content

Commit 1e06323

Browse files
CopilotBillWagner
andauthored
Document projection initializers (simplified names) for anonymous types (#47709)
* Initial plan * Document projection initializers (simplified names) for anonymous types Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> * Clarify duplicate member name explanation for projection initializers Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> * Add periods to code comments in anonymous types examples Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
1 parent af447e3 commit 1e06323

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

docs/csharp/fundamentals/types/anonymous-types.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ If you don't specify member names in the anonymous type, the compiler gives the
3838

3939
:::code language="csharp" source="snippets/anonymous-types/Program.cs" ID="snippet81":::
4040

41+
## Projection initializers
42+
43+
Anonymous types support *projection initializers*, which allow you to use local variables or parameters directly without explicitly specifying the member name. The compiler infers the member names from the variable names. The following example demonstrates this simplified syntax:
44+
45+
:::code language="csharp" source="snippets/anonymous-types/Program.cs" ID="ProjectionInitializers":::
46+
47+
This simplified syntax is particularly useful when creating anonymous types with many properties:
48+
49+
:::code language="csharp" source="snippets/anonymous-types/Program.cs" ID="ProjectionExample":::
50+
51+
The member name isn't inferred in the following cases:
52+
53+
- The candidate name is a member name of an anonymous type, such as `ToString` or `GetHashCode`.
54+
- The candidate name is a duplicate of another property member in the same anonymous type, either explicit or implicit.
55+
- The candidate name isn't a valid identifier (for example, it contains spaces or special characters).
56+
57+
In these cases, you must explicitly specify the member name.
58+
4159
> [!TIP]
4260
> You can use .NET style rule [IDE0037](../../../fundamentals/code-analysis/style-rules/ide0037.md) to enforce whether inferred or explicit member names are preferred.
4361

docs/csharp/fundamentals/types/snippets/anonymous-types/Program.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class Anonymous
1818
{
1919
static void Main()
2020
{
21-
// don't show this unless you add a bunch more
22-
// properties to the type. otherwise it obviates the
23-
// need for the anonymous type
21+
// Don't show this unless you add a bunch more
22+
// properties to the type. Otherwise it obviates the
23+
// need for the anonymous type.
2424
List<Product> products = new ()
2525
{
2626
new Product() { Color="Orange", Price=2.00M},
@@ -50,6 +50,34 @@ from prod in products
5050
var shipment = new { address = "Nowhere St.", product };
5151
var shipmentWithBonus = new { address = "Somewhere St.", product, bonus };
5252
// </Snippet03>
53+
54+
// <ProjectionInitializers>
55+
// Explicit member names.
56+
var personExplicit = new { FirstName = "Kyle", LastName = "Mit" };
57+
58+
// Projection initializers (inferred member names).
59+
var firstName = "Kyle";
60+
var lastName = "Mit";
61+
var personInferred = new { firstName, lastName };
62+
63+
// Both create equivalent anonymous types with the same property names.
64+
Console.WriteLine($"Explicit: {personExplicit.FirstName} {personExplicit.LastName}");
65+
Console.WriteLine($"Inferred: {personInferred.firstName} {personInferred.lastName}");
66+
// </ProjectionInitializers>
67+
68+
// <ProjectionExample>
69+
var title = "Software Engineer";
70+
var department = "Engineering";
71+
var salary = 75000;
72+
73+
// Using projection initializers.
74+
var employee = new { title, department, salary };
75+
76+
// Equivalent to explicit syntax:
77+
// var employee = new { title = title, department = department, salary = salary };
78+
79+
Console.WriteLine($"Title: {employee.title}, Department: {employee.department}, Salary: {employee.salary}");
80+
// </ProjectionExample>
5381
}
5482
}
5583
}

0 commit comments

Comments
 (0)