Skip to content

Commit a8c8d9f

Browse files
committed
restructure article
1 parent 31ede73 commit a8c8d9f

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ Anonymous types provide a convenient way to encapsulate a set of read-only prope
1515
> [!TIP]
1616
> In most scenarios, [tuples](../../language-reference/builtin-types/value-tuples.md) are the preferred choice over anonymous types. Tuples provide better performance, support deconstruction, and offer more flexible syntax. Use anonymous types primarily when you need expression tree support or when working with code that requires reference types.
1717
18-
You create anonymous types by using the [`new`](../../language-reference/operators/new-operator.md) operator together with an object initializer. For more information about object initializers, see [Object and Collection Initializers](../../programming-guide/classes-and-structs/object-and-collection-initializers.md).
19-
20-
The following example shows an anonymous type that is initialized with two properties named `Amount` and `Message`.
21-
22-
```csharp
23-
var v = new { Amount = 108, Message = "Hello" };
24-
25-
// Rest the mouse pointer over v.Amount and v.Message in the following
26-
// statement to verify that their inferred types are int and string.
27-
Console.WriteLine(v.Amount + v.Message);
28-
```
29-
30-
Anonymous types are typically used in the [`select`](../../language-reference/keywords/select-clause.md) clause of a query expression to return a subset of the properties from each object in the source sequence. For more information about queries, see [LINQ in C#](../../linq/index.md).
31-
32-
Anonymous types contain one or more public read-only properties. No other kinds of class members, such as methods or events, are valid. The expression that is used to initialize a property can't be `null`, an anonymous function, or a pointer type.
33-
3418
## Anonymous types vs tuples
3519

3620
Both anonymous types and tuples let you group multiple values without defining a named type. However, tuples are the preferred choice in most scenarios because they provide better performance and more flexibility. The following table summarizes the key differences:
@@ -45,7 +29,7 @@ Both anonymous types and tuples let you group multiple values without defining a
4529
| Access modifier | `internal` | `public` |
4630
| Member names | Required or inferred | Optional (with default names like `Item1`, `Item2`) |
4731

48-
### When to use tuples
32+
## When to use tuples
4933

5034
Use tuples when:
5135

@@ -58,7 +42,7 @@ The following example shows how tuples provide similar functionality to anonymou
5842

5943
:::code language="csharp" source="snippets/anonymous-types/Program.cs" ID="TupleExample":::
6044

61-
### When to use anonymous types
45+
## When to use anonymous types
6246

6347
Use anonymous types when:
6448

@@ -72,13 +56,15 @@ The most common scenario is to initialize an anonymous type with properties from
7256

7357
:::code language="csharp" source="snippets/anonymous-types/Program.cs" ID="ProductDefinition":::
7458

75-
The anonymous type declaration starts with the `new` keyword. The declaration initializes a new type that uses only two properties from `Product`. Using anonymous types causes a smaller amount of data to be returned in the query.
59+
The anonymous type declaration starts with the [`new`](../../language-reference/operators/new-operator.md) operator together with an [object initializer](../../programming-guide/classes-and-structs/object-and-collection-initializers.md). The declaration initializes a new type that uses only two properties from `Product`. Anonymous types are typically used in the [`select`](../../language-reference/keywords/select-clause.md) clause of a query expression to return a smaller amount of data. For more information about queries, see [LINQ in C#](../../linq/index.md).
7660

7761
If you don't specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them. You provide a name for a property that's being initialized with an expression, as shown in the previous example. In the following example, the names of the properties of the anonymous type are `Color` and `Price`. The instances are item from the `products` collection of `Product` types:
7862

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

81-
## Projection initializers
65+
Anonymous types contain one or more public read-only properties. No other kinds of class members, such as methods or events, are valid. The expression that is used to initialize a property can't be `null`, an anonymous function, or a pointer type.
66+
67+
## Projection initializers with anonymous types
8268

8369
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:
8470

0 commit comments

Comments
 (0)