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
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,9 @@ helpviewer_keywords:
12
12
13
13
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and isn't available at the source code level. The type of each property is inferred by the compiler.
14
14
15
+
> [!TIP]
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
+
15
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).
16
19
17
20
The following example shows an anonymous type that is initialized with two properties named `Amount` and `Message`.
@@ -28,6 +31,43 @@ Anonymous types are typically used in the [`select`](../../language-reference/ke
28
31
29
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.
30
33
34
+
## Anonymous types vs tuples
35
+
36
+
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:
37
+
38
+
| Feature | Anonymous types | Tuples |
39
+
|---------|----------------|--------|
40
+
| Type | Reference type (`class`) | Value type (`struct`) |
- You're working with expression trees (for example, in some LINQ providers).
66
+
- You need the object to be a reference type.
67
+
- You're projecting query results in LINQ and want named properties without defining a class.
68
+
69
+
For more information about choosing between anonymous types and tuples, see [Choosing between anonymous and tuple types](../../../standard/base-types/choosing-between-anonymous-and-tuple.md).
70
+
31
71
The most common scenario is to initialize an anonymous type with properties from another type. In the following example, assume that a class exists that is named `Product`. Class `Product` includes `Color` and `Price` properties, together with other properties that you aren't interested in:
0 commit comments