Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 126 additions & 69 deletions docs/csharp/tour-of-csharp/tutorials/branches-and-loops.md

Large diffs are not rendered by default.

154 changes: 102 additions & 52 deletions docs/csharp/tour-of-csharp/tutorials/hello-world.md

Large diffs are not rendered by default.

25 changes: 11 additions & 14 deletions docs/csharp/tour-of-csharp/tutorials/index.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
---
title: Interactive tutorials
description: Learn C# in your browser, and get started with your own development environment
ms.date: 04/23/2025
ms.date: 12/10/2025
---
# Introduction to C\#

Welcome to the introduction to C# tutorials. These lessons start with interactive code that you can run in your browser. You can learn the basics of C# from the [C# for Beginners video series](https://aka.ms/dotnet/beginnervideos/youtube/csharp) before starting these interactive lessons.
Welcome to the introduction to C# tutorials. These lessons start with interactive code that you can run in GitHub codespaces. You can learn the basics of C# from the [C# for Beginners video series](https://aka.ms/dotnet/beginnervideos/youtube/csharp) before starting these interactive lessons.

<!--markdownlint-disable-next-line MD034 -->
> [!VIDEO https://www.youtube.com/embed/9THmGiSPjBQ?si=3kUKFtOMLpEzeq7J]
The first lessons explain C# concepts using small snippets of code. You'll learn the basics of C# syntax and how to work with data types like strings, numbers, and booleans. It's all interactive, and you'll be writing and running code within minutes. These first lessons assume no prior knowledge of programming or the C# language. Each lesson builds on the prior lessons. You should do them in order. However, if you have some programming experience, you can skip or skim the first lessons and start with any new concepts.
The first lessons explain C# concepts by using small snippets of code. You learn the basics of C# syntax and how to work with data types like strings, numbers, and booleans. It's all interactive, and you'll be writing and running code within minutes. These first lessons assume no prior knowledge of programming or the C# language. Each lesson builds on the prior lessons. You should do them in order. However, if you have some programming experience, you can skip or skim the first lessons and start with any new concepts.

You can try these tutorials in different environments. The concepts you'll learn are the same. The difference is which experience you prefer:

- [In your browser, on the docs platform](hello-world.md): This experience embeds a runnable C# code window in docs pages. You write and execute C# code in the browser.
- [In the Microsoft Learn training experience](/training/paths/csharp-first-steps/). This learning path contains several modules that teach the basics of C#.
To use GitHub codespaces, you need to create a free [GitHub](https://github.com) account.

## Hello world

In the [Hello world](hello-world.md) tutorial, you'll create the most basic C# program. You'll explore the `string` type and how to work with text. You can also use the path on [Microsoft Learn training](/training/paths/csharp-first-steps/).
In the [Hello world](hello-world.md) tutorial, you create the most basic C# program. You explore the `string` type and how to work with text.

## Numbers in C\#

In the [Numbers in C#](numbers-in-csharp.md) tutorial, you'll learn how computers store numbers and how to perform calculations with different numeric types. You'll learn the basics of rounding, and how to perform mathematical calculations using C#.
In the [Numbers in C#](numbers-in-csharp.md) tutorial, you learn how computers store numbers and how to perform calculations with different numeric types. You learn the basics of rounding, and how to perform mathematical calculations using C#.

## Tuples and types

In the [Tuples and types](tuples-and-types.md) tutorial, you'll learn to create types in C#. You can create *tuples*, *records*, *struct*, and *class* types. The capabilities of these different kinds of types reflect their different uses.
In the [Tuples and types](tuples-and-types.md) tutorial, you learn to create types in C#. You can create *tuples*, *records*, *struct*, and *class* types. The capabilities of these different kinds of types reflect their different uses.

## Branches and loops

The [Branches and loops](branches-and-loops.md) tutorial teaches the basics of selecting different paths of code execution based on the values stored in variables. You'll learn the basics of control flow, which is the basis of how programs make decisions and choose different actions.
The [Branches and loops](branches-and-loops.md) tutorial teaches the basics of selecting different paths of code execution based on the values stored in variables. You learn the basics of control flow, which is the basis of how programs make decisions and choose different actions.

## List collection

The [List collection](list-collection.md) lesson gives you a tour of the List collection type that stores sequences of data. You'll learn how to add and remove items, search for items, and sort the lists. You'll explore different kinds of lists.
The [List collection](list-collection.md) lesson gives you a tour of the List collection type that stores sequences of data. You learn how to add and remove items, search for items, and sort the lists. You explore different kinds of lists.

## Pattern matching

The [Pattern matching](pattern-matching.md) lesson provides an introduction to *pattern matching*. Pattern matching enables you to compare an expression against a pattern. The success of the match determines which program logic to follow. Patterns can compare types, properties of a type, or contents of a list. You can combine multiple patterns using `and`, `or`, and `not` logic. Patterns provide a rich vocabulary to inspect data and make decisions in your program based on that inspection.
The [Pattern matching](pattern-matching.md) lesson provides an introduction to *pattern matching*. Pattern matching enables you to compare an expression against a pattern. The success of the match determines which program logic to follow. Patterns can compare types, properties of a type, or contents of a list. You can combine multiple patterns by using `and`, `or`, and `not` logic. Patterns provide a rich vocabulary to inspect data and make decisions in your program based on that inspection.

## Set up your local environment

After you finish these tutorials, set up a development environment. You'll want:
After you finish these tutorials, set up a development environment. You need:

[!INCLUDE [Prerequisites](../../../../includes/prerequisites-basic-winget.md)]
117 changes: 82 additions & 35 deletions docs/csharp/tour-of-csharp/tutorials/list-collection.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
---
title: Data collections - Introductory interactive tutorial
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.
ms.date: 03/07/2025
title: Data collections - Introductory tutorial
description: In this tutorial, you use GitHub Codespaces to learn about C# collections. You write C# code and see the results of compiling and running your code in the Codespaces environment.
ms.date: 12/10/2025
---
# Learn to manage data collections using List\<T> in C\#
# Tutorial: Learn to manage data collections using List\<T> in C\#

This introductory tutorial provides an introduction to the C# language and the basics of the class.
This introductory tutorial provides an introduction to the C# language and the basics of the <xref:System.Collections.Generic.List`1?displayProperty=nameWithType> class.

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

## A basic list example
In this tutorial, you:

> [!TIP]
> [!div class="checklist"]
>
> 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.
> * Launch a GitHub Codespace with a C# development environment.
> * Create your first C# app.
> * Create and use variables to store text data.
> * Use .NET APIs with text data.

To use codespaces, you need a GitHub account. If you don't already have one, you can create a free account at [GitHub.com](https://github.com).

## Prerequisites

You must have one of the following:

- A GitHub account to use [GitHub Codespaces](https://github.com/codespaces).
- A computer with the following tools installed:
- The [.NET 10 SDK](https://dotnet.microsoft.com/download/dotnet/10.0).
- [Visual Studio Code](https://code.visualstudio.com/download).
- The [C# DevKit](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit).

To use codespaces, you need a GitHub account. If you don't already have one, you can create a free account at [GitHub.com](https://github.com).

## A basic list example

To start a GitHub Codespace with the tutorial environment, open a browser window to the [tutorial codespace](https://github.com/dotnet/tutorial-codespace) repository. Select the green *Code* button, and the *Codespaces* tab. Then select the `+` sign to create a new Codespace using this environment. If you completed other tutorials in this series, you can open that codespace instead of creating a new one.

1. When your codespace loads, create a new file in the *tutorials* folder named *lists.cs*.
1. Open your new file.
1. Type or copy the following code into *lists.cs*:

:::code language="csharp" source="./snippets/ListCollection/list.cs" id="BasicList":::

Run the following code in the interactive window. Replace `<name>` with your name and select **Run**:
1. Run the code by typing the following command in the terminal window:

:::code language="csharp" interactive="try-dotnet-method" source="./snippets/ListCollection/Program.cs" id="BasicList":::
```dotnetcli
cd tutorials
dotnet lists.cs
```

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

The code to display names makes use of the [string interpolation](../../language-reference/tokens/interpolated.md) feature. When you precede a `string` with the `$` character, you can embed C# code in the string declaration. The actual string replaces that C# code with the value it generates. In this example, it replaces the `{name.ToUpper()}` with each name, converted to capital letters, because you called the <xref:System.String.ToUpper%2A?displayProperty=nameWithType> method.

Expand All @@ -29,47 +59,57 @@ Let's keep exploring.

The collection you created uses the <xref:System.Collections.Generic.List%601> type. This type stores sequences of elements. You specify the type of the elements between the angle brackets.

One important aspect of this <xref:System.Collections.Generic.List%601> type is that it can grow or shrink, enabling you to add or remove elements. You can see the results by modifying the contents after you displayed its contents. Add the following code after the code you already wrote (the loop that prints the contents):
One important aspect of this <xref:System.Collections.Generic.List%601> type is that it can grow or shrink, enabling you to add or remove elements. You can see the results by modifying the contents after you displayed its contents.

:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="ModifyList":::
1. Add the following code after the code you already wrote (the loop that prints the contents):

You added two more names to the end of the list. You also removed one as well. The output from this block of code shows the initial contents, then prints a blank line and the new contents.
:::code language="csharp" source="./snippets/ListCollection/list.cs" id="ModifyList":::

The <xref:System.Collections.Generic.List%601> enables you to reference individual items by **index** as well. You access items using the `[` and `]` tokens. Add the following code after what you already wrote and try it:
You added two more names to the end of the list. You also removed one as well. The output from this block of code shows the initial contents, then prints a blank line and the new contents.

:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="Indexers":::
1. The <xref:System.Collections.Generic.List%601> enables you to reference individual items by **index** as well. You access items using the `[` and `]` tokens. Add the following code after what you already wrote and try it:

You're not allowed to access past the end of the list. You can check how long the list is using the <xref:System.Collections.Generic.List%601.Count%2A> property. Add the following code:
:::code language="csharp" source="./snippets/ListCollection/list.cs" id="Indexers":::

:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="Property":::
You're not allowed to access past the end of the list. You can check how long the list is by using the <xref:System.Collections.Generic.List%601.Count%2A> property.

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.
1. Add the following code:

:::code language="csharp" source="./snippets/ListCollection/list.cs" id="Property":::

Type `dotnet lists.cs` again in the terminal window 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.

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

## Search and sort lists

Our samples use relatively small lists, but your applications might often create lists with many more elements, sometimes numbering in the thousands. To find elements in these larger collections, you need to search the list for different items. The <xref:System.Collections.Generic.List%601.IndexOf%2A> method searches for an item and returns the index of the item. If the item isn't in the list, `IndexOf` returns `-1`. Try it to see how it works. Add the following code after what you wrote so far:
Our samples use relatively small lists, but your applications might often create lists with many more elements, sometimes numbering in the thousands. To find elements in these larger collections, you need to search the list for different items. The <xref:System.Collections.Generic.List%601.IndexOf%2A> method searches for an item and returns the index of the item. If the item isn't in the list, `IndexOf` returns `-1`.

1. Try it to see how it works. Add the following code after what you wrote so far:

:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="Search":::
:::code language="csharp" source="./snippets/ListCollection/list.cs" id="Search":::

You might not know if an item is in the list, so you should always check the index returned by <xref:System.Collections.Generic.List%601.IndexOf%2A>. If it's `-1`, the item wasn't found.
You might not know if an item is in the list, so you should always check the index returned by <xref:System.Collections.Generic.List%601.IndexOf%2A>. If it's `-1`, the item wasn't found.

The items in your list can be sorted as well. The <xref:System.Collections.Generic.List%601.Sort%2A> method sorts all the items in the list in their normal order (alphabetically for strings). Add this code and run again:
1. You can also sort the items in your list. The <xref:System.Collections.Generic.List%601.Sort%2A> method sorts all the items in the list in their normal order (alphabetically for strings). Add this code and run again:

:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="Sort":::
:::code language="csharp" source="./snippets/ListCollection/list.cs" id="Sort":::

## Lists of other types

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:
So far, you've been using the `string` type in lists. Let's make a <xref:System.Collections.Generic.List%601> using a different type. Let's build a set of numbers.

1. Add the following code at the end of your source file:

:::code language="csharp" source="./snippets/ListCollection/list.cs" id="CreateList":::

:::code language="csharp" interactive="try-dotnet-method" source="./snippets/ListCollection/Program.cs" id="CreateList":::
That code 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.

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:
1. Add this code:

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

Press **Run** to see the results.
1. Type `dotnet lists.cs` in the terminal window to see the results.

## Challenge

Expand All @@ -80,16 +120,23 @@ Did you come up with something like this?
<!-- markdownlint-disable MD033 -->
<details>

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

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.
With each iteration of the loop, you take the last two integers in the list, sum them, and add that value to the list. The loop repeats until you add 20 items to the list.
</details>
<!-- markdownlint-disable MD033 -->

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.

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

- [Selecting a collection type](../../../standard/collections/selecting-a-collection-class.md)
- [Commonly used collection types](../../../standard/collections/commonly-used-collection-types.md)
- [When to use generic collections](../../../standard/collections/when-to-use-generic-collections.md)

## Cleanup resources

GitHub automatically deletes your Codespace after 30 days of inactivity. If you plan to explore more tutorials in this series, you can leave your Codespace provisioned. If you're ready to visit the [.NET site](https://dotnet.microsoft.com/download/dotnet) to download the .NET SDK, you can delete your Codespace. To delete your Codespace, open a browser window and navigate to [your Codespaces](https://github.com/codespaces). You should see a list of your codespaces in the window. Select the three dots (`...`) in the entry for the learn tutorial codespace and select **delete**.

## Next step

> [!div class="nextstepaction"]
> [Pattern matching](pattern-matching.md)
Loading
Loading