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
+7-62Lines changed: 7 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,68 +45,19 @@ The following example shows how tuples provide similar functionality to anonymou
45
45
46
46
You can deconstruct a tuple into separate variables, which provides a convenient way to work with individual tuple elements. C# supports several ways to deconstruct tuples:
47
47
48
-
```csharp
49
-
// Define a method that returns a tuple
50
-
(stringName, intAge, stringCity) GetPersonInfo()
51
-
{
52
-
return ("Alice", 30, "Seattle");
53
-
}
54
-
55
-
// Deconstruct using var for all variables
56
-
var (name, age, city) =GetPersonInfo();
57
-
Console.WriteLine($"{name} is {age} years old and lives in {city}");
58
-
// Output: Alice is 30 years old and lives in Seattle
A common use case for tuples is as a method return type. Instead of defining `out` parameters, you can group method results in a tuple. The following example demonstrates using tuples with dictionary lookups to return configuration ranges:
This pattern is useful when working with methods that need to return both a success indicator and multiple result values. The tuple allows you to use named fields (`Min` and `Max`) instead of generic names like `Item1` and `Item2`, making the code more readable and self-documenting.
112
63
@@ -158,9 +109,7 @@ Typically, when you use an anonymous type to initialize a variable, you declare
158
109
159
110
You can create an array of anonymously typed elements by combining an implicitly typed local variable and an implicitly typed array, as shown in the following example.
160
111
161
-
```csharp
162
-
varanonArray=new[] { new { name="apple", diam=4 }, new { name="grape", diam=1 }};
Anonymous types are [`class`](../../language-reference/keywords/class.md) types that derive directly from [`object`](../../language-reference/builtin-types/reference-types.md), and you can't cast them to any type except [`object`](../../language-reference/builtin-types/reference-types.md). The compiler provides a name for each anonymous type, although your application can't access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type.
166
115
@@ -180,11 +129,7 @@ Because the <xref:System.Object.Equals%2A> and <xref:System.Object.GetHashCode%2
180
129
181
130
Anonymous types do override the <xref:System.Object.ToString%2A> method, concatenating the name and `ToString` output of every property surrounded by curly braces.
182
131
183
-
```
184
-
var v = new { Title = "Hello", Age = 24 };
185
-
186
-
Console.WriteLine(v.ToString()); // "{ Title = Hello, Age = 24 }"
0 commit comments