Skip to content

Commit bcf3298

Browse files
authored
Add class declaration (#43693)
Fixes #43239 Add the class declaration for this type.
1 parent 9517dc6 commit bcf3298

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ Anonymous types are typically used in the [`select`](../../language-reference/ke
2828

2929
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 cannot be `null`, an anonymous function, or a pointer type.
3030

31-
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 are not interested in. Variable `products` is a collection of `Product` objects. 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.
31+
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 are not interested in:
3232

33-
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`.
33+
:::code language="csharp" source="snippets/anonymous-types/Program.cs" ID="ProductDefinition":::
34+
35+
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.
36+
37+
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:
3438

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
namespace anonymous_types
66
{
7+
// <ProductDefinition>
78
class Product
89
{
910
public string? Color {get;set;}
1011
public decimal Price {get;set;}
12+
public string? Name {get;set;}
13+
public string? Category {get;set;}
14+
public string? Size {get;set;}
1115
}
16+
// </ProductDefinition>
1217
class Anonymous
1318
{
1419
static void Main()

0 commit comments

Comments
 (0)