You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/fundamentals/types/anonymous-types.md
+6-20Lines changed: 6 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,22 +15,6 @@ Anonymous types provide a convenient way to encapsulate a set of read-only prope
15
15
> [!TIP]
16
16
> 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.
17
17
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
-
varv=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
-
34
18
## Anonymous types vs tuples
35
19
36
20
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
45
29
| Access modifier |`internal`|`public`|
46
30
| Member names | Required or inferred | Optional (with default names like `Item1`, `Item2`) |
47
31
48
-
###When to use tuples
32
+
## When to use tuples
49
33
50
34
Use tuples when:
51
35
@@ -58,7 +42,7 @@ The following example shows how tuples provide similar functionality to anonymou
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).
76
60
77
61
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:
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
82
68
83
69
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:
0 commit comments