Skip to content

Commit 072a9de

Browse files
committed
First edit pass on list collection tutorial
1 parent d9fbd6d commit 072a9de

File tree

3 files changed

+75
-86
lines changed

3 files changed

+75
-86
lines changed

docs/csharp/tour-of-csharp/tutorials/list-collection.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
---
2-
title: Data collections - Introductory interactive tutorial
2+
title: Data collections - Introductory tutorial
33
description: In this tutorial, you use your browser to learn about C# collections. You write C# code and see the results of compiling and running your code directly in the browser.
44
ms.date: 03/07/2025
55
---
66
# Learn to manage data collections using List\<T> in C\#
77

88
This introductory tutorial provides an introduction to the C# language and the basics of the class.
99

10-
This tutorial teaches you C# interactively, using your browser to write C# code and see the results of compiling and running your code. It contains a series of lessons that create, modify, and explore collections and arrays. You work primarily with the <xref:System.Collections.Generic.List%601> class.
10+
This tutorial teaches you C#. You write C# code and see the results of compiling and running that code. It contains a series of lessons that create, modify, and explore collections and arrays. You work primarily with the <xref:System.Collections.Generic.List%601> class.
1111

12-
## A basic list example
12+
<< TODO Create the app>>
1313

14-
> [!TIP]
15-
>
16-
> 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.
14+
## A basic list example
1715

18-
Run the following code in the interactive window. Replace `<name>` with your name and select **Run**:
16+
Add the following code to your source file. Replace `<name>` with your name. Then, type `dotnet run` to run the code:
1917

20-
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/ListCollection/Program.cs" id="BasicList":::
18+
:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="BasicList":::
2119

2220
You created a list of strings, added three names to that list, and printed the names in all CAPS. You're using concepts that you learned in earlier tutorials to loop through the list.
2321

@@ -43,7 +41,7 @@ You're not allowed to access past the end of the list. You can check how long th
4341

4442
:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="Property":::
4543

46-
Select **Run** again to see the results. In C#, indices start at 0, so the largest valid index is one less than the number of items in the list.
44+
Type `dotnet run` again to see the results. In C#, indices start at 0, so the largest valid index is one less than the number of items in the list.
4745

4846
For more information about indices, see the [Explore indexes and ranges](../../tutorials/ranges-indexes.md) article.
4947

@@ -61,15 +59,15 @@ The items in your list can be sorted as well. The <xref:System.Collections.Gener
6159

6260
## Lists of other types
6361

64-
You've been using the `string` type in lists so far. Let's make a <xref:System.Collections.Generic.List%601> using a different type. Let's build a set of numbers. Delete the code you wrote so far, and replace it with the following code:
62+
You've been using the `string` type in lists so far. Let's make a <xref:System.Collections.Generic.List%601> using a different type. Let's build a set of numbers. Add the following code at the end of your source file:
6563

66-
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/ListCollection/Program.cs" id="CreateList":::
64+
:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="CreateList":::
6765

6866
That creates a list of integers, and sets the first two integers to the value 1. The *Fibonacci Sequence*, a sequence of numbers, starts with two 1's. Each next Fibonacci number is found by taking the sum of the previous two numbers. Add this code:
6967

7068
:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="Fibonacci":::
7169

72-
Press **Run** to see the results.
70+
Type `dotnet run` to see the results.
7371

7472
## Challenge
7573

@@ -80,13 +78,13 @@ Did you come up with something like this?
8078
<!-- markdownlint-disable MD033 -->
8179
<details>
8280

83-
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/ListCollection/Program.cs" id="Answer":::
81+
:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="Answer":::
8482

8583
With each iteration of the loop, you're taking the last two integers in the list, summing them, and adding that value to the list. The loop repeats until you added 20 items to the list.
8684
</details>
8785
<!-- markdownlint-disable MD033 -->
8886

89-
You completed the list interactive tutorial, the final introduction to C# interactive tutorial. 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. Or, you can continue with the [Explore object oriented programming with classes and objects](../../fundamentals/tutorials/classes.md) tutorial.
87+
You completed the list tutorial. You can continue with the [Pattern matching](./pattern-matching.md) tutorial.
9088

9189
You can learn more about [.NET collections](../../../standard/collections/index.md) in the following articles:
9290

docs/csharp/tour-of-csharp/tutorials/snippets/ListCollection/ListCollection.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/snippets/ListCollection/Program.cs

Lines changed: 62 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,78 @@
1-

2-
ListOfStrings();
3-
ListOfNumbers();
4-
ChallengeAnswer();
5-
6-
void ListOfStrings()
1+
// <BasicList>
2+
List<string> names = ["<name>", "Ana", "Felipe"];
3+
foreach (var name in names)
74
{
8-
// <BasicList>
9-
List<string> names = ["<name>", "Ana", "Felipe"];
10-
foreach (var name in names)
11-
{
12-
Console.WriteLine($"Hello {name.ToUpper()}!");
13-
}
14-
// </BasicList>
5+
Console.WriteLine($"Hello {name.ToUpper()}!");
6+
}
7+
// </BasicList>
158

16-
// <ModifyList>
17-
Console.WriteLine();
18-
names.Add("Maria");
19-
names.Add("Bill");
20-
names.Remove("Ana");
21-
foreach (var name in names)
22-
{
23-
Console.WriteLine($"Hello {name.ToUpper()}!");
24-
}
25-
// </ModifyList>
9+
// <ModifyList>
10+
Console.WriteLine();
11+
names.Add("Maria");
12+
names.Add("Bill");
13+
names.Remove("Ana");
14+
foreach (var name in names)
15+
{
16+
Console.WriteLine($"Hello {name.ToUpper()}!");
17+
}
18+
// </ModifyList>
2619

27-
// <Indexers>
28-
Console.WriteLine($"My name is {names[0]}.");
29-
Console.WriteLine($"I've added {names[2]} and {names[3]} to the list.");
30-
// </Indexers>
20+
// <Indexers>
21+
Console.WriteLine($"My name is {names[0]}.");
22+
Console.WriteLine($"I've added {names[2]} and {names[3]} to the list.");
23+
// </Indexers>
3124

32-
// <Property>
33-
Console.WriteLine($"The list has {names.Count} people in it");
34-
// </Property>
25+
// <Property>
26+
Console.WriteLine($"The list has {names.Count} people in it");
27+
// </Property>
3528

36-
// <Search>
37-
var index = names.IndexOf("Felipe");
38-
if (index == -1)
39-
{
40-
Console.WriteLine($"When an item is not found, IndexOf returns {index}");
41-
}
42-
else
43-
{
44-
Console.WriteLine($"The name {names[index]} is at index {index}");
45-
}
46-
47-
index = names.IndexOf("Not Found");
48-
if (index == -1)
49-
{
50-
Console.WriteLine($"When an item is not found, IndexOf returns {index}");
51-
}
52-
else
53-
{
54-
Console.WriteLine($"The name {names[index]} is at index {index}");
55-
}
56-
// </Search>
29+
// <Search>
30+
var index = names.IndexOf("Felipe");
31+
if (index == -1)
32+
{
33+
Console.WriteLine($"When an item is not found, IndexOf returns {index}");
34+
}
35+
else
36+
{
37+
Console.WriteLine($"The name {names[index]} is at index {index}");
38+
}
5739

58-
// <Sort>
59-
names.Sort();
60-
foreach (var name in names)
61-
{
62-
Console.WriteLine($"Hello {name.ToUpper()}!");
63-
}
64-
// </Sort>
40+
index = names.IndexOf("Not Found");
41+
if (index == -1)
42+
{
43+
Console.WriteLine($"When an item is not found, IndexOf returns {index}");
44+
}
45+
else
46+
{
47+
Console.WriteLine($"The name {names[index]} is at index {index}");
6548
}
49+
// </Search>
6650

67-
void ListOfNumbers()
51+
// <Sort>
52+
names.Sort();
53+
foreach (var name in names)
6854
{
69-
// <CreateList>
70-
List<int> fibonacciNumbers = [1, 1];
71-
// </CreateList>
55+
Console.WriteLine($"Hello {name.ToUpper()}!");
56+
}
57+
// </Sort>
7258

73-
// <Fibonacci>
74-
var previous = fibonacciNumbers[fibonacciNumbers.Count - 1];
75-
var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2];
59+
// <CreateList>
60+
List<int> fibonacciNumbers = [1, 1];
61+
// </CreateList>
7662

77-
fibonacciNumbers.Add(previous + previous2);
63+
// <Fibonacci>
64+
var previous = fibonacciNumbers[fibonacciNumbers.Count - 1];
65+
var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2];
7866

79-
foreach (var item in fibonacciNumbers)
80-
{
81-
Console.WriteLine(item);
82-
}
83-
// </Fibonacci>
67+
fibonacciNumbers.Add(previous + previous2);
68+
69+
foreach (var item in fibonacciNumbers)
70+
{
71+
Console.WriteLine(item);
8472
}
73+
// </Fibonacci>
74+
75+
ChallengeAnswer();
8576

8677
void ChallengeAnswer()
8778
{

0 commit comments

Comments
 (0)