Skip to content

Commit 1f0befb

Browse files
committed
another sample pass
1 parent 5b9ea09 commit 1f0befb

File tree

4 files changed

+35
-24
lines changed

4 files changed

+35
-24
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
public static class SampleTwo
44
{
55
// <PointVersion2>
6-
public record Point(int X, int Y)
7-
{
8-
public double Slope() => (double)Y / (double)X;
9-
}
6+
public record Point(int X, int Y);
107

118
public static void Main()
129
{
1310
Point pt = new Point(1, 1);
14-
double slope = pt.Slope();
15-
Console.WriteLine($"The slope of {pt} is {slope}");
11+
var pt2 = pt with { Y = 10 };
12+
Console.WriteLine($"The two points are {pt} and {pt2}");
1613
}
1714
// </PointVersion2>
1815
}

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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,17 @@ 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-class" source="./snippets/TuplesAndTypes/Program.cs" id="DeclareRecord":::
52+
:::code language="csharp" interactive="try-dotnet-class" source="./snippets/TuplesAndTypes/Program.cs" id="PointVersion2":::
5353

5454
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:
5555

5656
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="CreateRecord":::
5757

5858
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:
5959

60-
:::code language="csharp" source="./snippets/TuplesAndTypes/PointEvolution.cs" id="PointVersion2":::
60+
:::code language="csharp" source="./snippets/TuplesAndTypes/PointStruct.cs" id="PointVersion3":::
61+
62+
:::code language="csharp" source="./snippets/TuplesAndTypes/PointStruct.cs" id="UseSlope":::
6163

6264
The `Main` method shows how you can create and use this record type.
6365

0 commit comments

Comments
 (0)