Skip to content

Commit 2f5c344

Browse files
committed
correct interactive bits.
Use the class type, and declare a `Main` method.
1 parent 706a3e3 commit 2f5c344

File tree

4 files changed

+40
-37
lines changed

4 files changed

+40
-37
lines changed
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
namespace PointEvolution;
22

3-
// <PointVersion2>
4-
public record Point(int X, int Y)
3+
public static class SampleTwo
54
{
6-
public double Slope() => (double)Y / (double)X;
7-
}
8-
// </PointVersion2>
5+
// <PointVersion2>
6+
public record Point(int X, int Y);
97

10-
public static class Expected
11-
{
12-
public static void Example()
8+
public static void Main()
139
{
14-
// <Version2Usage>
1510
Point pt = new Point(1, 1);
16-
double slope = pt.Slope();
17-
Console.WriteLine($"The slope of {pt} is {slope}");
18-
// </Version2Usage>
11+
var pt2 = pt with { Y = 10 };
12+
Console.WriteLine($"The two points are {pt} and {pt2}");
1913
}
14+
// </PointVersion2>
2015
}

docs/csharp/tour-of-csharp/tutorials/snippets/TuplesAndTypes/PointStruct.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1-
namespace MoreStruct;
1+
namespace FinalRecord
2+
{
3+
public static class SampleThree
4+
{
5+
// <PointVersion3>
6+
public record Point(int X, int Y)
7+
{
8+
public double Slope() => (double)Y / (double)X;
9+
}
10+
// </PointVersion3>
11+
public static void Main()
12+
{
13+
Point pt = new Point(1, 1);
14+
var pt2 = pt with { Y = 10 };
15+
// <UseSlope>
16+
double slope = pt.Slope();
17+
Console.WriteLine($"The slope of {pt} is {slope}");
18+
// </UseSlope>
19+
}
20+
}
21+
}
222

3-
// <RecordStruct>
4-
public record struct Point(int X, int Y)
23+
namespace MoreStruct
524
{
6-
public double Slope() => (double) Y / (double) X;
25+
// <RecordStruct>
26+
public record struct Point(int X, int Y)
27+
{
28+
public double Slope() => (double) Y / (double) X;
29+
}
30+
// </RecordStruct>
731
}
8-
// </RecordStruct>

docs/csharp/tour-of-csharp/tutorials/snippets/TuplesAndTypes/Program.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,3 @@ void FirstExample()
3535
// </TupleTypes>
3636
}
3737

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>
48-

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,15 @@ Tuples are easy to create, but they are limited in their capabilities. Tuple typ
4949

5050
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:
5151

52-
:::code language="csharp" interactive="try-dotnet-program" source="./snippets/TuplesAndTypes/Program.cs" id="DeclareRecord":::
52+
:::code language="csharp" interactive="try-dotnet-class" source="./snippets/TuplesAndTypes/PointEvolution.cs" id="PointVersion2":::
5353

54-
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:
54+
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:
5555

56-
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="CreateRecord":::
56+
:::code language="csharp" source="./snippets/TuplesAndTypes/PointStruct.cs" id="PointVersion3":::
5757

58-
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:
58+
:::code language="csharp" source="./snippets/TuplesAndTypes/PointStruct.cs" id="UseSlope":::
5959

60-
:::code language="csharp" source="./snippets/TuplesAndTypes/PointEvolution.cs" id="PointVersion2":::
61-
62-
You can call it like the following:
63-
64-
:::code language="csharp" source="./snippets/TuplesAndTypes/PointEvolution.cs" id="Version2Usage":::
60+
The `Main` method shows how you can create and use this record type.
6561

6662
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:
6763

0 commit comments

Comments
 (0)