Skip to content

Commit 711e8c0

Browse files
committed
add samples
1 parent 850191c commit 711e8c0

File tree

3 files changed

+82
-50
lines changed

3 files changed

+82
-50
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace PointEvolution;
2+
3+
// <PointVersion2>
4+
public record Point(int X, int Y)
5+
{
6+
public double Slope() => (double)Y / (double)X;
7+
}
8+
// </PointVersion2>
9+
10+
public static class Expected
11+
{
12+
public static void Example()
13+
{
14+
// <Version2Usage>
15+
Point pt = new Point(1, 1);
16+
double slope = pt.Slope();
17+
Console.WriteLine($"The slope of {pt} is {slope}");
18+
// </Version2Usage>
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
1-
// See https://aka.ms/new-console-template for more information
2-
Console.WriteLine("Hello, World!");
3-
4-
// 1. Use tuples. Consider a point?
5-
// 2. Convert tuple to a record.
6-
// 3. Make it a record, add some method, like distance?
7-
// 4. discuss struct and class. Those add more behavior.2nd project?
8-
// arbitrary distance between two points.
9-
// slope?
1+

2+
using System.Xml.Linq;
3+
4+
FirstExample();
5+
6+
void FirstExample()
7+
{
8+
// <CreateTuple>
9+
var pt = (X: 1, Y: 2);
10+
11+
var slope = (double)pt.Y / (double)pt.X;
12+
Console.WriteLine($"A line from the origin to the point {pt} has a slope of {slope}.");
13+
// </CreateTuple>
14+
15+
// <Modify>
16+
pt.X = pt.X + 5;
17+
Console.WriteLine($"The point is now at {pt}.");
18+
// </Modify>
19+
20+
// <Wither>
21+
var pt2 = pt with { Y = 10 };
22+
Console.WriteLine($"The point 'pt2' is at {pt2}.");
23+
// </Wither>
24+
25+
// <NamedAssignment>
26+
var subscript = (A: 0, B: 0);
27+
subscript = pt;
28+
Console.WriteLine(subscript);
29+
// </NamedAssignment>
30+
31+
// <TupleTypes>
32+
var namedData = (Name: "Morning observation", Temp: 17, Wind: 4);
33+
var person = (FirstName: "", LastName: "");
34+
var order = (Product: "guitar picks", style: "triangle", quantity: 500, UnitPrice: 0.10m);
35+
// </TupleTypes>
36+
}
37+
38+
// <CreateRecord>
39+
Point pt = new Point(1, 1);
40+
var pt2 = pt with { Y = 10 };
41+
// </CreateRecord>
42+
43+
PointEvolution.Expected.Example();
44+
45+
// <DeclareRecord>
46+
public record Point(int X, int Y);
47+
// </DeclareRecord>
1048

docs/csharp/tour-of-csharp/tutorials/tuples-and-types.md

+15-41
Original file line numberDiff line numberDiff line change
@@ -17,77 +17,51 @@ The preceding tutorials worked with text and numbers. Strings and Numbers are *s
1717

1818
*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:
1919

20-
```csharp
21-
var pt = (X: 1, Y: 2);
22-
23-
var slope = (double)pt.Y / (double)pt.X;
24-
Console.WriteLine($"A line from the origin to the point {pt} has a slope of {slope}");
25-
```
20+
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/TuplesAndTypes/Program.cs" id="CreateTuple":::
2621

2722
> [!TIP]
2823
>
2924
> 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.
3025
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-
var subscript(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:
26+
You can reassign any member of a tuple:
3827

39-
```csharp
40-
pt.X = pt.X + 5;
41-
```
28+
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="Modify":::
4229

4330
You can also create a new tuple that's a modified copy of the original using a `with` expression:
4431

45-
```csharp
46-
var pt2 = pt with { Y = 10 };
47-
```
32+
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="Wither":::
4833

4934
The tuple `pt2` contains the `X` value of `pt` (6), and `pt2.Y` is 10.
5035

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:
37+
38+
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="NamedAssignment":::
39+
40+
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+
5142
Tuples are easy to create: You declare multiple members enclosed in parentheses. All the following declare different tuples of different arites and member types:
5243

53-
```csharp
54-
var namedData = (Name= "Morning observation", Temp = 17, Wind = 4);
55-
var person = (FirstName = "", LastName = "");
56-
var order = (Product = "guitar picks", style = "triangle", quantity = 500, UnitPrice = 0.10m);
57-
```
44+
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="TupleTypes":::
5845

5946
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.
6047

6148
## Create record types
6249

6350
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:
6451

65-
```csharp
66-
public record Point(int X, int Y);
67-
```
52+
:::code language="csharp" interactive="try-dotnet-program" source="./snippets/TuplesAndTypes/Program.cs" id="DeclareRecord":::
6853

6954
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:
7055

71-
```csharp
72-
Point pt = new Point {X = 1, Y = 1};
73-
var pt2 = pt with { Y = 10 };
74-
```
56+
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="CreateRecord":::
7557

7658
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:
7759

78-
```csharp
79-
public record Point(int X, int Y)
80-
{
81-
public double Slope() => (double) Y / (double) X;
82-
}
83-
```
60+
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="PointVersion2":::
8461

8562
You can call it like the following:
8663

87-
```csharp
88-
double slope = pt.Slope();
89-
Console.WriteLine($"The slope of {pt} is {slope}");
90-
```
64+
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="Version2Usage":::
9165

9266
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:
9367

0 commit comments

Comments
 (0)