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/programming-guide/classes-and-structs/anonymous-types.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,23 +10,23 @@ ai-usage: ai-assisted
10
10
---
11
11
# Anonymous types (C# Programming Guide)
12
12
13
-
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without defining a named type first. The compiler generates a type name at compile time that isn't accessible in your source code. The compiler infers the type of each property.
13
+
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without defining a named type first. The compiler generates a type name at compile time that you can't access in your source code. The compiler infers the type of each property.
14
14
15
15
Create anonymous types by using the [`new`](../../language-reference/operators/new-operator.md) operator together with an object initializer. The following example shows an anonymous type that's initialized with two properties, `Name` and `Age`:
You can specify property names explicitly with`Name = value` syntax. When you initialize an anonymous type with a variable or member access expression, the compiler infers the property name from that expression:
21
+
You can specify property names explicitly by using the`Name = value` syntax. When you initialize an anonymous type with a variable or member access expression, the compiler infers the property name from that expression:
In the preceding example, the compiler infers `productName` and `price` as the property names from the variable names used in the initializer.
26
26
27
27
## Declare anonymous types with var
28
28
29
-
Because the type name is generated by the compiler and isn't available in source code, you must use [`var`](../../language-reference/statements/declarations.md#implicitly-typed-local-variables) to declare the local variable. You can't specify the type name explicitly:
29
+
Because the compiler generates the type name and you can't access it in source code, you must use [`var`](../../language-reference/statements/declarations.md#implicitly-typed-local-variables) to declare the local variable. You can't specify the type name explicitly:
30
30
31
31
```csharp
32
32
// You must use var — you can't write a named type here.
0 commit comments