Skip to content

Commit f9f2d0e

Browse files
committed
First pass of Tuples and types
First edit pass on tuples and types.
1 parent fcc9f12 commit f9f2d0e

File tree

5 files changed

+78
-103
lines changed

5 files changed

+78
-103
lines changed

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

Lines changed: 0 additions & 15 deletions
This file was deleted.

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

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,56 @@
1-

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

6-
void FirstExample()
50+
namespace PrimaryRecord
751
{
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>
52+
// <PointRecord>
53+
public record Point(int X, int Y);
54+
// </PointRecord>
3655
}
3756

docs/csharp/tour-of-csharp/tutorials/snippets/TuplesAndTypes/TuplesAndTypes.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
---
2-
title: Tuples and types - Introductory interactive tutorial
3-
description: In this tutorial about creating types, you use your browser to learn C# interactively. You're going to write C# code and see the results of compiling and running your code directly in the browser.
4-
ms.date: 04/03/2025
2+
title: Tuples and types - Introductory tutorial
3+
description: This tutorial teaches you to create types in C#. You write C# code and see the results of compiling and running your code as you learn the structure of types.
4+
ms.date: 12/03/2025
55
---
66
# Create types in C\#
77

88
This tutorial teaches you about creating types in C#. You write small amounts of code, then you compile and run that code. The tutorial contains a series of lessons that explore different kinds of types in C#. These lessons teach you the fundamentals of the C# language.
99

10-
> [!TIP]
11-
> When a code snippet block includes the "Run" button, that button opens the interactive window, or replaces the existing code in the interactive window. When the snippet doesn't include a "Run" button, you can copy the code and add it to the current interactive window.
12-
1310
The preceding tutorials worked with text and numbers. Strings and Numbers are *simple types*: They each store one single value. As your programs grow larger, you need to work with more sophisticated data structures. C# provides different kinds of types you can define when you need data structures with more fields, properties, or behavior. Let's start to explore those types.
1411

12+
TODO: Create sample.
13+
1514
## Tuples
1615

17-
*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. Select the "Run" button to paste the following code into the interactive window and run it.
16+
*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. Copy the following code to your file and type `dotnet run`.
1817

19-
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/TuplesAndTypes/Program.cs" id="CreateTuple":::
18+
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="CreateTuple":::
2019

2120
> [!TIP]
22-
> As you explore C# (or any programming language), you make mistakes when you write code. The **compiler** finds those errors and reports 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.
21+
> As you explore C# (or any programming language), you make mistakes when you write code. The **compiler** finds those errors and reports them to you. When the output contains error messages, look closely at the example code and your code to see what to fix. That exercise helps you learn the structure of C# code. You can also ask Copilot to find differences or spot any mistakes.
2322
24-
You can reassign any member of a tuple. Add the following code in the interactive window after the existing code. Press "Run" again to see the results.
23+
You can reassign any member of a tuple. Add the following code after the existing code. Type `dotnet run` again to see the results.
2524

2625
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="Modify":::
2726

28-
You can also create a new tuple that's a modified copy of the original using a `with` expression. Add the following code after the code already in the interactive window and press "Run" to see the results:
27+
You can also create a new tuple that's a modified copy of the original using a `with` expression. Add the following code after the existing code and type `dotnet run` to see the results:
2928

3029
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="Wither":::
3130

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

34-
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. You can add the following code after the code you already wrote in the interactive window and try it:
33+
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. You can add the following code after the code you already wrote and try it:
3534

3635
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="NamedAssignment":::
3736

@@ -41,32 +40,35 @@ Tuples are easy to create: You declare multiple members enclosed in parentheses.
4140

4241
:::code language="csharp" source="./snippets/TuplesAndTypes/Program.cs" id="TupleTypes":::
4342

44-
Tuples are easy to create, but they're limited in their capabilities. Tuple types don't have names, so you can't convey meaning to the set of values. Tuple types can't add behavior. C# has other kinds of types you can create when your type defines behavior.
43+
While tuples are easy to create, they're limited in their capabilities. Tuple types don't have names, so you can't convey meaning to the set of values. Tuple types can't add behavior. C# has other kinds of types you can create when your type defines behavior.
4544

4645
## Create record types
4746

4847
Tuples are great for those times when you want multiple values in the same structure. They're lightweight, and can be declared as they're 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 following code sample uses a `Main` method to represent the entry point for the program. That way, you can declare a `record` type preceding the entry point in the code. Press the "Run" button on the following code to replace your existing sample with the following code.
4948

50-
> [!WARNING]
51-
> Don't copy and paste. The interactive window must be reset to run the following sample. If you make a mistake, the window hangs, and you need to refresh the page to continue.
49+
The following code declares and uses a `record` type to represent a `Point`:
50+
51+
:::code language="csharp" source="./snippets/TuplesAndTypes/PointEvolution.cs" id="PointRecord":::
52+
53+
The preceding code must be at the bottom of your source file. Type declarations like `record` declarations must follow executable statements in a file-based app.
5254

53-
The following code declares and uses a `record` type to represent a `Point`, and then uses that `Point` structure in the `Main` method:
55+
Add the following code preceding the `record` declaration:
5456

55-
:::code language="csharp" interactive="try-dotnet-class" source="./snippets/TuplesAndTypes/PointEvolution.cs" id="PointVersion2":::
57+
:::code language="csharp" source="./snippets/TuplesAndTypes/PointEvolution.cs" id="UsePointRecord":::
5658

57-
The `record` declaration is a single line of code for the `Point` type that stores the values `X` and `Y` in readonly properties. You use the name `Point` wherever you use that type. Properly named types, like `Point`, provide information about how the type is used. The `Main` method shows how to use a `with` expression to create a new point that's a modified copy of the existing point. The line `pt2 = pt with { Y = 10 }` says "`pt2` has the same values as `pt` except that `Y` is assigned to 10." You can add any number of properties to change in a single `with` expression.
59+
The `record` declaration is a single line of code for the `Point` type that stores the values `X` and `Y` in readonly properties. You use the name `Point` wherever you use that type. Properly named types, like `Point`, provide information about how the type is used. The additional code shows how to use a `with` expression to create a new point that's a modified copy of the existing point. The line `pt4 = pt3 with { Y = 10 }` says "`pt4` has the same values as `pt3` except that `Y` is assigned to 10." You can add any number of properties to change in a single `with` expression.
5860

59-
The preceding `record` declaration is a single line of code that ends in `;`, like all C# statements. You can add behavior to a `record` type by declaring *members*. A record member can be a function, or more data elements. The members of a type are in the type declaration, between `{` and `}` characters. Replace the record declaration you made with the following code:
61+
The preceding `record` declaration is a single line of code that ends in `;`. You can add behavior to a `record` type by declaring *members*. A record member can be a function, or more data elements. The members of a type are in the type declaration, between `{` and `}` characters. Delete the `;` and add the following lines of code after the `record` declaration:
6062

61-
:::code language="csharp" source="./snippets/TuplesAndTypes/PointStruct.cs" id="PointVersion3":::
63+
:::code language="csharp" source="./snippets/TuplesAndTypes/PointStruct.cs" id="AddSlopeMethod":::
6264

63-
Then, add the following code to the `Main` method after the line containing the `with` expression:
65+
Then, add the following code before the `record` declaration, after the line containing the `with` expression:
6466

6567
:::code language="csharp" source="./snippets/TuplesAndTypes/PointStruct.cs" id="UseSlope":::
6668

6769
You added 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 that includes extra behavior. You can modify the `Point` type to make it a `record struct` as well:
6870

69-
:::code language="csharp" source="./snippets/TuplesAndTypes/PointStruct.cs" id="RecordStruct":::
71+
:::code language="csharp" source="./snippets/TuplesAndTypes/PointStruct.cs" id="RecordStructPoint":::
7072

7173
A `record struct` is a `struct` type that includes the extra behavior added to all `record` types.
7274

@@ -86,7 +88,7 @@ You can also define `interface` types to declare behavioral contracts that diffe
8688

8789
You typically use all these types in larger programs and libraries. Once you install the .NET SDK, you can explore those types using tutorials on [classes](../../fundamentals/tutorials/classes.md) in the fundamentals section.
8890

89-
You completed the "Create types in C#" interactive tutorial. You can select the **Branches and Loops** link to start the next interactive tutorial, or you can visit the [.NET site](https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/intro) to download the .NET SDK, create a project on your machine, and keep coding. The "Next steps" section brings you back to these tutorials.
91+
You completed the "Create types in C#" tutorial. You can select the **Branches and Loops** link to start the next tutorial.
9092

9193
You can learn more about types in C# in the following articles:
9294

0 commit comments

Comments
 (0)