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/tour-of-csharp/tutorials/tuples-and-types.md
+15-41
Original file line number
Diff line number
Diff line change
@@ -17,77 +17,51 @@ The preceding tutorials worked with text and numbers. Strings and Numbers are *s
17
17
18
18
*Tuples* are an ordered sequence of values with a fixed length. Each element of a tuple has a type and an optional name. The following code declares a tuple that represents a 2D point:
19
19
20
-
```csharp
21
-
varpt= (X: 1, Y: 2);
22
-
23
-
varslope= (double)pt.Y/ (double)pt.X;
24
-
Console.WriteLine($"A line from the origin to the point {pt} has a slope of {slope}");
> As you explore C# (or any programming language), you make mistakes when you write code. The **compiler** finds those errors and report them to you. When the output contains error messages, look closely at the example code, and the code in the interactive window to see what to fix. That exercise helps you learn the structure of C# code.
30
25
31
-
Tuples are structural types. In other words, different tuple types don't have names like `string` or `int`. A tuple type is defined by the number of members, referred to as *arity* and the types of those members. The member names are for convenience. You can assign a tuple to a tuple with the same shape even if the members have different names:
32
-
33
-
```csharp
34
-
varsubscript(A, B) =pt;
35
-
```
36
-
37
-
The variable `subscript` has two members, but of which are integers. Both `subscript` and `pt` represent instances of the same tuple type: a tuple containing 2 `int` members. You can reassign any member of a tuple:
The tuple `pt2` contains the `X` value of `pt` (6), and `pt2.Y` is 10.
50
35
36
+
Tuples are structural types. In other words, tuple types don't have names like `string` or `int`. A tuple type is defined by the number of members, referred to as *arity* and the types of those members. The member names are for convenience. You can assign a tuple to a tuple with the same arity and types even if the members have different names:
The variable `subscript` has two members, both of which are integers. Both `subscript` and `pt` represent instances of the same tuple type: a tuple containing 2 `int` members.
41
+
51
42
Tuples are easy to create: You declare multiple members enclosed in parentheses. All the following declare different tuples of different arites and member types:
Tuples are easy to create, but they are limited in their capabilities. Tuple types don't have names, so you can convey meaning to the set of values. Tuple types can't add behavior. For that, C# has other kinds of types you can create.
60
47
61
48
## Create record types
62
49
63
50
Tuples are great for those times when you want multiple values in the same structure. They're lightweight, and can be declared as they are used. As your program goes, you might find that you use the same tuple type throughout your code. If your app does work in the 2D graph space, the tuples that represent points might be common. Once you find that, you can declare a `record` type that stores those values and provides more capabilities:
The preceding single line of code declares a named *record* type that stores the values `X` and `Y` in readonly properties. You use the name `Point` wherever you use that type. Using a named type makes it clear how the type is used. Unlike tuples, you can't change the value of a property in a record, but you can still make a new copy using a `with` expression:
Record types can include behavior as well as data. In C#, any type declaration starts with `{` and ends with `}`. Replace the record declaration you made with the following:
You've added a bit of formality to the *tuple* representing an `X` and `Y` value. You made it a `record` that defined a named type, and included a member to calculate the slope. A `record` type is a shorthand for a `record class`: A `class` type where some types are generated by the compiler. You can modify the `Point` type to make it a `record struct` as well:
0 commit comments