From ab757352e33f95eea190f5b4775ae4c5df269769 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 12 Oct 2017 16:23:56 -0400 Subject: [PATCH 1/8] add List collection quick start --- .../quick-starts/branches-and-loops.yml | 2 + docs/csharp/quick-starts/index.md | 7 + docs/csharp/quick-starts/list-collection.yml | 162 ++++++++++++++++++ docs/csharp/quick-starts/toc.md | 1 + 4 files changed, 172 insertions(+) create mode 100644 docs/csharp/quick-starts/list-collection.yml diff --git a/docs/csharp/quick-starts/branches-and-loops.yml b/docs/csharp/quick-starts/branches-and-loops.yml index c1e96d1c67dd4..ce5ead10b914c 100644 --- a/docs/csharp/quick-starts/branches-and-loops.yml +++ b/docs/csharp/quick-starts/branches-and-loops.yml @@ -8,6 +8,8 @@ metadata: level: Beginner displayType: two-column interactive: csharp + nextTutorialHref: arrays-and-collections + nextTutorialTitle: Arrays and colletions in C# items: - durationInMinutes: 1 content: | diff --git a/docs/csharp/quick-starts/index.md b/docs/csharp/quick-starts/index.md index 12b1f04126fbc..f83b3b0a68061 100644 --- a/docs/csharp/quick-starts/index.md +++ b/docs/csharp/quick-starts/index.md @@ -44,3 +44,10 @@ different actions. This beginner lesson assumes that you have finished the [Hello World](hello-world.yml) and [Numbers in C#](numbers-in-csharp.yml) lessons. + +## [List collection](list-collection.yml) + +The [List collection](list-collection.yml) 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 remove items and sort the lists. You'll explore different kinds of lists. + +This beginner lesson assumes that you have finished the quick starts listed above. diff --git a/docs/csharp/quick-starts/list-collection.yml b/docs/csharp/quick-starts/list-collection.yml new file mode 100644 index 0000000000000..744ed07662aa0 --- /dev/null +++ b/docs/csharp/quick-starts/list-collection.yml @@ -0,0 +1,162 @@ +### YamlMime:YamlDocument +documentType: Tutorial +title: Collections in C# +metadata: + title: Collections in C#. Learn to use sequences and collections in C#. + description: In this tutorial, you'll use your browser to learn C# interactively. You write C# code and see the results of compiling and running your code directly in the browser. + audience: Developer + level: Beginner + displayType: two-column + interactive: csharp +items: +- durationInMinutes: 1 + content: | + This tutorial teaches you C# interactively, using your browser to write C# and see the results of compiling and running your code. It contains a series of lessons that create, modify and explore collections and arrays. +- title: Create lists + durationInMinutes: 2 + content: | + Run the following code in the interactive window. To do that, type code block in the interactive window and click the **Run** button: + + ```csharp + var names = new List { "", "Ana", "Felipe" }; + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } + ``` + + You've just created a list of strings, added three names to that list, and printed out the names in all CAPS. You're using concepts + that you've learned in earlier quick starts to loop through the list. + + Another new feature here is **interpolated strings**. When you preced 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 `ToUpper` method. + + Let's keep exploring. + + > [!NOTE] + > This online coding experience is in preview mode. If you encounter problems, please report them [on the dotnet/try repo](https://github.com/dotnet/try/issues). + +- title: Modify list contents + durationInMinutes: 3 + content: | + The collection you created using the type. This type stores sequences of elements. You specify the type of the elements between the angle brackets. + + One important aspect of this type is that it can grow or shrink, enabling you to add or remove elements. Add this code below the code you've already written: + + ```csharp + Console.WriteLine(); + names.Add("Maria"); + names.Add("Bill"); + names.Remove("Ana"); + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } + ``` + + You've added two more names to the end of the list. You've also removed one as well. + + The enables you to reference individual items by **index** as well. C# uses 0 for the first index. Add this code to the bottom and try it: + + ```csharp + Console.WriteLine($"My name is {names[0]}"); + Console.WriteLine($"I've added {names[2]} and {names[3]} to the list"); + ``` + + It's not allowed to access past the end of the list. You can check how long the list is using the `` property: + + ```csharp + Console.WriteLine($"The list has {names.Count} people in it"); + ``` + + Click **Run** again to see the results. + + > [!NOTE] + > This online coding experience is in preview mode. If you encounter problems, please report them [on the dotnet/try repo](https://github.com/dotnet/try/issues). + +- title: Search lists + durationInMinutes: 5 + content: | + Our samples use relatively small lists, but in your applications you may often create lists with many more elements, sometimes numbering well over the thousands. To find elements in these larger collections, you need to search the list for different items. The method searches for an item and returns the index of the item. Try this to see how it works: + + ```csharp + var index = names.IndexOf("Felipe"); + Console.WriteLine($"The name {names[index]} is at index {index}"); + + var notFound = names.IndexOf("Not Found"); + Console.WriteLine($"When an item is not found, IndexOf returns {notFound}"); + ``` + + The items in your list can be sorted as well. The method sorts all the items in the list in their normal order (alphabetically in the case of strings): + + ```csharp + names.Sort(); + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } + ``` + + > [!NOTE] + > This online coding experience is in preview mode. If you encounter problems, please report them [on the dotnet/try repo](https://github.com/dotnet/try/issues). + +- title: Lists of other types + durationInMinutes: 5 + content: | + You've been using the `string` type in lists so far. Let's make a using a different type. Let's build a set of numbers. Delete the code you wrote so far, and replace it with this: + + ```csharp + var fibonacciNumbers = new List {1, 1,}; + ``` + + That creates a list of integers, and sets the first two integers to the value 1. These are the first two values of a *Fibonacci Sequence*, a sequence of numbers. Each next Fibonacci number is found by taking the sum of the previous two numbers. Add this code: + + ```csharp + var previous = fibonacciNumbers[fibonacciNumbers.Count - 1]; + var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2]; + + fibonacciNumbers.Add(previous + previous2); + + foreach(var item in fibonacciNumbers) + Console.WriteLine(item); + ``` + + Press **Run** to see the results; + + > [!NOTE] + > This online coding experience is in preview mode. If you encounter problems, please report them [on the dotnet/try repo](https://github.com/dotnet/try/issues). + +- title: Challenge + durationInMinutes: 10 + content: | + See if you can put together some of the lessons from this and earlier lessons. Expand on what you've built so far with Fibonacci Numbers. Try and write the code to generate the first 20 numbers in the sequence. + + > [!NOTE] + > This online coding experience is in preview mode. If you encounter problems, please report them [on the dotnet/try repo](https://github.com/dotnet/try/issues). + +- title: Complete challenge + durationInMinutes: 3 + content: | + Did you come up with something like this? + + ```csharp + var fibonacciNumbers = new List {1, 1,}; + + while (fibonacciNumbers.Count < 20) + { + var previous = fibonacciNumbers[fibonacciNumbers.Count - 1]; + var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2]; + + fibonacciNumbers.Add(previous + previous2); + } + foreach(var item in fibonacciNumbers) + Console.WriteLine(item); + ``` + + > [!NOTE] + > This online coding experience is in preview mode. If you encounter problems, please report them [on the dotnet/try repo](https://github.com/dotnet/try/issues). + +- content: | + You've completed the list tutorial. + + You can learn more about working with the `List` type in the + [.NET Guide](../../standard/index.md) topic on [collections](../../standard/collections/index.md). You'll also learn about many other collection types. diff --git a/docs/csharp/quick-starts/toc.md b/docs/csharp/quick-starts/toc.md index e1a47364f54ca..bbb3a19eba526 100644 --- a/docs/csharp/quick-starts/toc.md +++ b/docs/csharp/quick-starts/toc.md @@ -2,3 +2,4 @@ ## [Hello world](hello-world.yml) ## [Numbers in C#](numbers-in-csharp.yml) ## [Branches and loops](branches-and-loops.yml) +## [List collections](list-collection.yml) From 0407347032317dff2dffcecd130f5041a5ad1ba3 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 13 Oct 2017 10:46:26 -0400 Subject: [PATCH 2/8] respond to feedback --- docs/csharp/quick-starts/hello-world.yml | 2 +- docs/csharp/quick-starts/list-collection.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/csharp/quick-starts/hello-world.yml b/docs/csharp/quick-starts/hello-world.yml index a5aec737d468e..b4b5ba19bff5b 100644 --- a/docs/csharp/quick-starts/hello-world.yml +++ b/docs/csharp/quick-starts/hello-world.yml @@ -17,7 +17,7 @@ items: - title: Run your first C# program durationInMinutes: 2 content: | - Run the following code in the interactive window. To do that, type code block in the interactive window and click the **Run** button: + Run the following code in the interactive window. To do that, type the following code block in the interactive window and click the **Run** button: ```csharp Console.WriteLine("Hello World!"); diff --git a/docs/csharp/quick-starts/list-collection.yml b/docs/csharp/quick-starts/list-collection.yml index 744ed07662aa0..250efcabce508 100644 --- a/docs/csharp/quick-starts/list-collection.yml +++ b/docs/csharp/quick-starts/list-collection.yml @@ -15,7 +15,7 @@ items: - title: Create lists durationInMinutes: 2 content: | - Run the following code in the interactive window. To do that, type code block in the interactive window and click the **Run** button: + Run the following code in the interactive window. To do that, type the following code block in the interactive window and click the **Run** button: ```csharp var names = new List { "", "Ana", "Felipe" }; @@ -28,7 +28,7 @@ items: You've just created a list of strings, added three names to that list, and printed out the names in all CAPS. You're using concepts that you've learned in earlier quick starts to loop through the list. - Another new feature here is **interpolated strings**. When you preced 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 `ToUpper` method. + Another new feature here is **interpolated strings**. 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 `ToUpper` method. Let's keep exploring. From 79591c764936bbf6e1309dfb77804dc778ef6f56 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 13 Oct 2017 12:31:33 -0400 Subject: [PATCH 3/8] add offline version of list quick start This version runs on your own development environment. --- .../quick-starts/arrays-and-collections.md | 154 ++++++++++++++++++ docs/csharp/quick-starts/index.md | 6 +- samples/csharp/list-quickstart/Program.cs | 65 ++++++++ .../list-quickstart/list-quickstart.csproj | 8 + 4 files changed, 231 insertions(+), 2 deletions(-) create mode 100644 docs/csharp/quick-starts/arrays-and-collections.md create mode 100644 samples/csharp/list-quickstart/Program.cs create mode 100644 samples/csharp/list-quickstart/list-quickstart.csproj diff --git a/docs/csharp/quick-starts/arrays-and-collections.md b/docs/csharp/quick-starts/arrays-and-collections.md new file mode 100644 index 0000000000000..0bdc4db75d247 --- /dev/null +++ b/docs/csharp/quick-starts/arrays-and-collections.md @@ -0,0 +1,154 @@ +--- +title: Quick Start - Arrays and Collections - C# Guide +description: Learn C# in your browser +keywords: C#, Get Started, tutorial, collections, List +author: billwagner +ms.author: wiwagn +ms.date: 10/13/2017 +ms.topic: article +ms.prod: .net +ms.technology: devlang-csharp +ms.devlang: csharp +--- +# C# Quick start: Arrays and Collections # + +This tutorial provides an introduction of the C# language and the basics of the +class. + +## A simple list example. + +Create a directory named **list-quickstart** and run `dotnet new console`. + +Open **program.cs** in your favorite editor, and replace the existing code with the following: + +```csharp +using System; +using System.Collections.Generic; + +namespace list_quickstart +{ + class Program + { + static void Main(string[] args) + { + var names = new List { "", "Ana", "Felipe" }; + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } + } + } +} +``` + +> [!NOTE] +> If you are starting from the code you wrote in [dot.net](https://dot.net/) you will already have that code written. + +Replace `` with your name. Save **Program.cs**. Type `dotnet run` in your console window to try it. + +You've just created a list of strings, added three names to that list, and printed out the names in all CAPS. You're using concepts that you've learned in earlier quick starts to loop through the list. + +Another new feature here is **interpolated strings**. 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 `ToUpper` method. + +Let's keep exploring. + +## Modify list contents + +The collection you created using the type. This type stores sequences of elements. You specify the type of the elements between the angle brackets. + +One important aspect of this type is that it can grow or shrink, enabling you to add or remove elements. Add this code before the closing `}` in the `Main` method: + +```csharp +Console.WriteLine(); +names.Add("Maria"); +names.Add("Bill"); +names.Remove("Ana"); +foreach (var name in names) +{ + Console.WriteLine($"Hello {name.ToUpper()}!"); +} +``` + +You've added two more names to the end of the list. You've also removed one as well. Save the file, and type `dotnet run` to try it. + +The enables you to reference individual items by **index** as well. C# uses 0 for the first index. Add this code directly below the code you just added and try it: + +```csharp +Console.WriteLine($"My name is {names[0]}"); +Console.WriteLine($"I've added {names[2]} and {names[3]} to the list"); +``` + +It's not allowed to access past the end of the list. You can check how long the list is using the `` property. Add the following code at the end of the Main method: + +```csharp +Console.WriteLine($"The list has {names.Count} people in it"); + ``` + +Save the file, and type `dotnet run` again to see the results. + +## Search lists +Our samples use relatively small lists, but in your applications you may often create lists with many more elements, sometimes numbering well over the thousands. To find elements in these larger collections, you need to search the list for different items. The method searches for an item and returns the index of the item. Add this code to the bottom of your `Main` method: + +```csharp +var index = names.IndexOf("Felipe"); +Console.WriteLine($"The name {names[index]} is at index {index}"); + +var notFound = names.IndexOf("Not Found"); +Console.WriteLine($"When an item is not found, IndexOf returns {notFound}"); +``` + +The items in your list can be sorted as well. The method sorts all the items in the list in their normal order (alphabetically in the case of strings). Add this code to the bottom of our `Main` method: + +```csharp +names.Sort(); +foreach (var name in names) +{ + Console.WriteLine($"Hello {name.ToUpper()}!"); +} +``` + +Save the file and type `dotnet run` to try this latest version. + +Before you start the next section, let's move the current code into a separate method. That makes it easier to start working with a new example. Rename your `Main` method to `WorkingWithStrings` and write a new `Main` methdo that calls `WorkingWithStrings`. When you have finished, your code should look like this: + +[!code-csharp[WorkingWithStrings](../../../samples/csharp/list-quickstart/program.cs#WorkingWithStrings "Working with Strings")] + + +## Lists of other types + +You've been using the `string` type in lists so far. Let's make a using a different type. Let's build a set of numbers. + +Add the following to the bottom of your new `Main` method: + +```csharp +var fibonacciNumbers = new List {1, 1,}; +``` + +That creates a list of integers, and sets the first two integers to the value 1. These are the first two values of a *Fibonacci Sequence*, a sequence of numbers. Each next Fibonacci number is found by taking the sum of the previous two numbers. Add this code: + +```csharp +var previous = fibonacciNumbers[fibonacciNumbers.Count - 1]; +var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2]; + +fibonacciNumbers.Add(previous + previous2); + +foreach(var item in fibonacciNumbers) + Console.WriteLine(item); +``` + +Save the file and type `dotnet run` to see the results. + +> [!TIP] +> To concentrate on just this section, you can comment out the code that calls `WorkingWithStrings();`. Just put two `/` characters in front of the call like this: `// WorkingWithStrings();`. + +## Challenge +See if you can put together some of the lessons from this and earlier lessons. Expand on what you've built so far with Fibonacci Numbers. Try and write the code to generate the first 20 numbers in the sequence. + +## Complete challenge + +You can see an example solution by [looking at the finished sample code on GitHub](https://github.com/dotnet/docs/tree/master/samples/csharp/list-quickstart/Program.cs) + +You've completed the list tutorial. + +You can learn more about working with the `List` type in the +[.NET Guide](../../standard/index.md) topic on [collections](../../standard/collections/index.md). You'll also learn about many other collection types. diff --git a/docs/csharp/quick-starts/index.md b/docs/csharp/quick-starts/index.md index f83b3b0a68061..367b45cf6726b 100644 --- a/docs/csharp/quick-starts/index.md +++ b/docs/csharp/quick-starts/index.md @@ -1,10 +1,10 @@ --- title: Quick Starts - C# Guide -description: Learn C# in your browser +description: Learn C# in your browser, and get started with your own development environment keywords: C#, Get Started, Lessons, Interactive author: billwagner ms.author: wiwagn -ms.date: 09/18s/2017 +ms.date: 09/18/2017 ms.topic: article ms.prod: .net ms.technology: devlang-csharp @@ -51,3 +51,5 @@ The [List collection](list-collection.yml) 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 remove items and sort the lists. You'll explore different kinds of lists. This beginner lesson assumes that you have finished the quick starts listed above. + +You can also try this lesson [using your own develoment environment on your machine](arrays-and-collections.md). diff --git a/samples/csharp/list-quickstart/Program.cs b/samples/csharp/list-quickstart/Program.cs new file mode 100644 index 0000000000000..4f7263d40cfc1 --- /dev/null +++ b/samples/csharp/list-quickstart/Program.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; + +namespace list_quickstart +{ + class Program + { +#region WorkWithStrings + static void Main(string[] args) + { + //WorkingWithStrings(); + + var fibonacciNumbers = new List {1, 1,}; + + fibonacciNumbers.Add(previous + previous2); + + while (fibonacciNumbers.Count < 20) + { + var previous = fibonacciNumbers[fibonacciNumbers.Count - 1]; + var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2]; + + fibonacciNumbers.Add(previous + previous2); + } + foreach(var item in fibonacciNumbers) + Console.WriteLine(item); + + } + + public static void WorkingWithStrings() + { + var names = new List { "", "Ana", "Felipe" }; + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } + + Console.WriteLine(); + names.Add("Maria"); + names.Add("Bill"); + names.Remove("Ana"); + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } + + Console.WriteLine($"My name is {names[0]}"); + Console.WriteLine($"I've added {names[2]} and {names[3]} to the list"); + + Console.WriteLine($"The list has {names.Count} people in it"); + + var index = names.IndexOf("Felipe"); + Console.WriteLine($"The name {names[index]} is at index {index}"); + + var notFound = names.IndexOf("Not Found"); + Console.WriteLine($"When an item is not found, IndexOf returns {notFound}"); + + names.Sort(); + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } + } +#endregion + } +} diff --git a/samples/csharp/list-quickstart/list-quickstart.csproj b/samples/csharp/list-quickstart/list-quickstart.csproj new file mode 100644 index 0000000000000..ce1697ae88377 --- /dev/null +++ b/samples/csharp/list-quickstart/list-quickstart.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.0 + + + From ed85adacdecfa4bb469220bb71af564477f9f8d0 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 13 Oct 2017 13:35:36 -0400 Subject: [PATCH 4/8] Fix the include tag name --- samples/csharp/list-quickstart/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/csharp/list-quickstart/Program.cs b/samples/csharp/list-quickstart/Program.cs index 4f7263d40cfc1..1b48564f0de82 100644 --- a/samples/csharp/list-quickstart/Program.cs +++ b/samples/csharp/list-quickstart/Program.cs @@ -5,7 +5,7 @@ namespace list_quickstart { class Program { -#region WorkWithStrings +#region WorkingWithStrings static void Main(string[] args) { //WorkingWithStrings(); From ae87d721af8a58c9a9b7b31ba599f5509d907736 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Sat, 14 Oct 2017 16:47:06 -0400 Subject: [PATCH 5/8] respond to feedback. --- docs/csharp/quick-starts/arrays-and-collections.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/csharp/quick-starts/arrays-and-collections.md b/docs/csharp/quick-starts/arrays-and-collections.md index 0bdc4db75d247..05a59bf1a3fd1 100644 --- a/docs/csharp/quick-starts/arrays-and-collections.md +++ b/docs/csharp/quick-starts/arrays-and-collections.md @@ -1,6 +1,6 @@ --- title: Quick Start - Arrays and Collections - C# Guide -description: Learn C# in your browser +description: Learn C# by exploring the List collection in this quick start. keywords: C#, Get Started, tutorial, collections, List author: billwagner ms.author: wiwagn @@ -17,6 +17,10 @@ class. ## A simple list example. +> [!NOTE] +> If you are starting from the code you wrote in [dot.net](https://dot.net/) you will already have the code written in this section. Jump to [Modify list contents](#modify-list-contents). + + Create a directory named **list-quickstart** and run `dotnet new console`. Open **program.cs** in your favorite editor, and replace the existing code with the following: @@ -41,9 +45,6 @@ namespace list_quickstart } ``` -> [!NOTE] -> If you are starting from the code you wrote in [dot.net](https://dot.net/) you will already have that code written. - Replace `` with your name. Save **Program.cs**. Type `dotnet run` in your console window to try it. You've just created a list of strings, added three names to that list, and printed out the names in all CAPS. You're using concepts that you've learned in earlier quick starts to loop through the list. @@ -54,7 +55,7 @@ Let's keep exploring. ## Modify list contents -The collection you created using the type. This type stores sequences of elements. You specify the type of the elements between the angle brackets. +The collection you created uses the type. This type stores sequences of elements. You specify the type of the elements between the angle brackets. One important aspect of this type is that it can grow or shrink, enabling you to add or remove elements. Add this code before the closing `}` in the `Main` method: From 3954b8a168fe720975ab27abf06331641e11ab1c Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 16 Oct 2017 14:20:39 -0400 Subject: [PATCH 6/8] respond to feedback @rpetrusha I've addressed all your comments. --- .../quick-starts/arrays-and-collections.md | 90 +++++++++++++++---- docs/csharp/quick-starts/index.md | 6 +- docs/csharp/quick-starts/list-collection.yml | 21 +++-- samples/csharp/list-quickstart/Program.cs | 7 +- 4 files changed, 91 insertions(+), 33 deletions(-) diff --git a/docs/csharp/quick-starts/arrays-and-collections.md b/docs/csharp/quick-starts/arrays-and-collections.md index 05a59bf1a3fd1..c15d0ca28617a 100644 --- a/docs/csharp/quick-starts/arrays-and-collections.md +++ b/docs/csharp/quick-starts/arrays-and-collections.md @@ -1,5 +1,5 @@ --- -title: Quick Start - Arrays and Collections - C# Guide +title: Quick Start - Collections - C# Guide description: Learn C# by exploring the List collection in this quick start. keywords: C#, Get Started, tutorial, collections, List author: billwagner @@ -10,15 +10,15 @@ ms.prod: .net ms.technology: devlang-csharp ms.devlang: csharp --- -# C# Quick start: Arrays and Collections # +# C# Quick start: Collections # -This tutorial provides an introduction of the C# language and the basics of the +This tutorial provides an introduction to the C# language and the basics of the class. ## A simple list example. > [!NOTE] -> If you are starting from the code you wrote in [dot.net](https://dot.net/) you will already have the code written in this section. Jump to [Modify list contents](#modify-list-contents). +> If you are starting from the code you wrote in [dot.net](https://dot.net/), you will already have the code written in this section. Jump to [Modify list contents](#modify-list-contents). Create a directory named **list-quickstart** and run `dotnet new console`. @@ -38,7 +38,7 @@ namespace list_quickstart var names = new List { "", "Ana", "Felipe" }; foreach (var name in names) { - Console.WriteLine($"Hello {name.ToUpper()}!"); + Console.WriteLine($"Hello {name.ToUpper()}!"); } } } @@ -49,7 +49,7 @@ Replace `` with your name. Save **Program.cs**. Type `dotnet run` in your You've just created a list of strings, added three names to that list, and printed out the names in all CAPS. You're using concepts that you've learned in earlier quick starts to loop through the list. -Another new feature here is **interpolated strings**. 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 `ToUpper` method. +The code to display names makes use of **interpolated strings**. 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 method. Let's keep exploring. @@ -72,14 +72,14 @@ foreach (var name in names) You've added two more names to the end of the list. You've also removed one as well. Save the file, and type `dotnet run` to try it. -The enables you to reference individual items by **index** as well. C# uses 0 for the first index. Add this code directly below the code you just added and try it: +The enables you to reference individual items by **index** as well. You place the index between `[` and `]` tokens following the list name. C# uses 0 for the first index. Add this code directly below the code you just added and try it: ```csharp Console.WriteLine($"My name is {names[0]}"); Console.WriteLine($"I've added {names[2]} and {names[3]} to the list"); ``` -It's not allowed to access past the end of the list. You can check how long the list is using the `` property. Add the following code at the end of the Main method: +You cannot access an index beyond the end of the list. Remember that indices start at 0, so the largest valid index is one less than the number of items in the list. You can check how long the list is using the `` property. Add the following code at the end of the Main method: ```csharp Console.WriteLine($"The list has {names.Count} people in it"); @@ -87,15 +87,28 @@ Console.WriteLine($"The list has {names.Count} people in it"); Save the file, and type `dotnet run` again to see the results. -## Search lists -Our samples use relatively small lists, but in your applications you may often create lists with many more elements, sometimes numbering well over the thousands. To find elements in these larger collections, you need to search the list for different items. The method searches for an item and returns the index of the item. Add this code to the bottom of your `Main` method: +## Search and sort lists +Our samples use relatively small lists, but your applications may 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 method searches for an item and returns the index of the item. Add this code to the bottom of your `Main` method: ```csharp var index = names.IndexOf("Felipe"); -Console.WriteLine($"The name {names[index]} is at index {index}"); +if (index == -1) +{ + Console.WriteLine($"When an item is not found, IndexOf returns {index}"); +} else +{ + Console.WriteLine($"The name {names[index]} is at index {index}"); +} -var notFound = names.IndexOf("Not Found"); -Console.WriteLine($"When an item is not found, IndexOf returns {notFound}"); +index = names.IndexOf("Not Found"); +if (index == -1) +{ + Console.WriteLine($"When an item is not found, IndexOf returns {index}"); +} else +{ + Console.WriteLine($"The name {names[index]} is at index {index}"); + +} ``` The items in your list can be sorted as well. The method sorts all the items in the list in their normal order (alphabetically in the case of strings). Add this code to the bottom of our `Main` method: @@ -110,10 +123,49 @@ foreach (var name in names) Save the file and type `dotnet run` to try this latest version. -Before you start the next section, let's move the current code into a separate method. That makes it easier to start working with a new example. Rename your `Main` method to `WorkingWithStrings` and write a new `Main` methdo that calls `WorkingWithStrings`. When you have finished, your code should look like this: +Before you start the next section, let's move the current code into a separate method. That makes it easier to start working with a new example. Rename your `Main` method to `WorkingWithStrings` and write a new `Main` method that calls `WorkingWithStrings`. When you have finished, your code should look like this: + +```csharp +static void Main(string[] args) +{ + WorkingWithStrings(); +} + +public static void WorkingWithStrings() +{ + var names = new List { "", "Ana", "Felipe" }; + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } + + Console.WriteLine(); + names.Add("Maria"); + names.Add("Bill"); + names.Remove("Ana"); + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } + + Console.WriteLine($"My name is {names[0]}"); + Console.WriteLine($"I've added {names[2]} and {names[3]} to the list"); + + Console.WriteLine($"The list has {names.Count} people in it"); -[!code-csharp[WorkingWithStrings](../../../samples/csharp/list-quickstart/program.cs#WorkingWithStrings "Working with Strings")] + var index = names.IndexOf("Felipe"); + Console.WriteLine($"The name {names[index]} is at index {index}"); + var notFound = names.IndexOf("Not Found"); + Console.WriteLine($"When an item is not found, IndexOf returns {notFound}"); + + names.Sort(); + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } +} +``` ## Lists of other types @@ -122,7 +174,7 @@ You've been using the `string` type in lists so far. Let's make a {1, 1,}; +var fibonacciNumbers = new List {1, 1}; ``` That creates a list of integers, and sets the first two integers to the value 1. These are the first two values of a *Fibonacci Sequence*, a sequence of numbers. Each next Fibonacci number is found by taking the sum of the previous two numbers. Add this code: @@ -137,7 +189,7 @@ foreach(var item in fibonacciNumbers) Console.WriteLine(item); ``` -Save the file and type `dotnet run` to see the results. +Save the file and type `dotnet run` to see the results. > [!TIP] > To concentrate on just this section, you can comment out the code that calls `WorkingWithStrings();`. Just put two `/` characters in front of the call like this: `// WorkingWithStrings();`. @@ -149,7 +201,9 @@ See if you can put together some of the lessons from this and earlier lessons. E You can see an example solution by [looking at the finished sample code on GitHub](https://github.com/dotnet/docs/tree/master/samples/csharp/list-quickstart/Program.cs) -You've completed the list tutorial. +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've added 20 items to the list. + +Congratulations, you've completed the list tutorial. You can learn more about working with the `List` type in the [.NET Guide](../../standard/index.md) topic on [collections](../../standard/collections/index.md). You'll also learn about many other collection types. diff --git a/docs/csharp/quick-starts/index.md b/docs/csharp/quick-starts/index.md index 367b45cf6726b..f2238457286b9 100644 --- a/docs/csharp/quick-starts/index.md +++ b/docs/csharp/quick-starts/index.md @@ -42,7 +42,7 @@ different paths of code based on the values stored in variables. You'll learn th basics of control flow, which is the basics of how programs make decisions and choose different actions. -This beginner lesson assumes that you have finished the [Hello World](hello-world.yml) and +This beginning lesson assumes that you have finished the [Hello World](hello-world.yml) and [Numbers in C#](numbers-in-csharp.yml) lessons. ## [List collection](list-collection.yml) @@ -50,6 +50,6 @@ This beginner lesson assumes that you have finished the [Hello World](hello-worl The [List collection](list-collection.yml) 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 remove items and sort the lists. You'll explore different kinds of lists. -This beginner lesson assumes that you have finished the quick starts listed above. +This beginning lesson assumes that you have finished the quick starts listed above. -You can also try this lesson [using your own develoment environment on your machine](arrays-and-collections.md). +You can also try this lesson [using the own develoment environment on your machine](arrays-and-collections.md). diff --git a/docs/csharp/quick-starts/list-collection.yml b/docs/csharp/quick-starts/list-collection.yml index 250efcabce508..f65385bfa74c9 100644 --- a/docs/csharp/quick-starts/list-collection.yml +++ b/docs/csharp/quick-starts/list-collection.yml @@ -28,7 +28,7 @@ items: You've just created a list of strings, added three names to that list, and printed out the names in all CAPS. You're using concepts that you've learned in earlier quick starts to loop through the list. - Another new feature here is **interpolated strings**. 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 `ToUpper` method. + The code to display names makes use of **interpolated strings**. 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 method. Let's keep exploring. @@ -55,37 +55,40 @@ items: You've added two more names to the end of the list. You've also removed one as well. - The enables you to reference individual items by **index** as well. C# uses 0 for the first index. Add this code to the bottom and try it: + The enables you to reference individual items by **index** as well. You access items using the `[` and `]` tokens. Add this code to the bottom and try it: ```csharp Console.WriteLine($"My name is {names[0]}"); Console.WriteLine($"I've added {names[2]} and {names[3]} to the list"); ``` - It's not allowed to access past the end of the list. You can check how long the list is using the `` property: + You're not allowed to access past the end of the list. You can check how long the list is using the `` property: ```csharp Console.WriteLine($"The list has {names.Count} people in it"); ``` - Click **Run** again to see the results. + Click **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. > [!NOTE] > This online coding experience is in preview mode. If you encounter problems, please report them [on the dotnet/try repo](https://github.com/dotnet/try/issues). -- title: Search lists +- title: Search and sort lists durationInMinutes: 5 content: | - Our samples use relatively small lists, but in your applications you may often create lists with many more elements, sometimes numbering well over the thousands. To find elements in these larger collections, you need to search the list for different items. The method searches for an item and returns the index of the item. Try this to see how it works: + Our samples use relatively small lists, but your applications may 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 method searches for an item and returns the index of the item. Try this to see how it works: ```csharp var index = names.IndexOf("Felipe"); - Console.WriteLine($"The name {names[index]} is at index {index}"); + if (index != -1) + Console.WriteLine($"The name {names[index]} is at index {index}"); var notFound = names.IndexOf("Not Found"); - Console.WriteLine($"When an item is not found, IndexOf returns {notFound}"); + Console.WriteLine($"When an item is not found, IndexOf returns {notFound}"); ``` + You may not know if an item is in the list, so you should always check the index returned by . If it is -1, the item was not found. + The items in your list can be sorted as well. The method sorts all the items in the list in their normal order (alphabetically in the case of strings): ```csharp @@ -152,6 +155,8 @@ items: Console.WriteLine(item); ``` + 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've added 20 items to the list. + > [!NOTE] > This online coding experience is in preview mode. If you encounter problems, please report them [on the dotnet/try repo](https://github.com/dotnet/try/issues). diff --git a/samples/csharp/list-quickstart/Program.cs b/samples/csharp/list-quickstart/Program.cs index 1b48564f0de82..84d2d77db916b 100644 --- a/samples/csharp/list-quickstart/Program.cs +++ b/samples/csharp/list-quickstart/Program.cs @@ -8,9 +8,9 @@ class Program #region WorkingWithStrings static void Main(string[] args) { - //WorkingWithStrings(); + WorkingWithStrings(); - var fibonacciNumbers = new List {1, 1,}; + var fibonacciNumbers = new List {1, 1}; fibonacciNumbers.Add(previous + previous2); @@ -23,7 +23,6 @@ static void Main(string[] args) } foreach(var item in fibonacciNumbers) Console.WriteLine(item); - } public static void WorkingWithStrings() @@ -31,7 +30,7 @@ public static void WorkingWithStrings() var names = new List { "", "Ana", "Felipe" }; foreach (var name in names) { - Console.WriteLine($"Hello {name.ToUpper()}!"); + Console.WriteLine($"Hello {name.ToUpper()}!"); } Console.WriteLine(); From 3274168d4450fd00d5169da3e3bb10292bab80b2 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 16 Oct 2017 16:21:57 -0400 Subject: [PATCH 7/8] respond to final feedback --- .../quick-starts/arrays-and-collections.md | 72 +++++++++++-------- docs/csharp/quick-starts/list-collection.yml | 22 +++--- samples/csharp/list-quickstart/Program.cs | 2 - 3 files changed, 52 insertions(+), 44 deletions(-) diff --git a/docs/csharp/quick-starts/arrays-and-collections.md b/docs/csharp/quick-starts/arrays-and-collections.md index c15d0ca28617a..e03eb628693a5 100644 --- a/docs/csharp/quick-starts/arrays-and-collections.md +++ b/docs/csharp/quick-starts/arrays-and-collections.md @@ -18,12 +18,13 @@ class. ## A simple list example. > [!NOTE] -> If you are starting from the code you wrote in [dot.net](https://dot.net/), you will already have the code written in this section. Jump to [Modify list contents](#modify-list-contents). +> If you're starting from the code you wrote in [dot.net](https://dot.net/), you already have the code written in this section. Jump to [Modify list contents](#modify-list-contents). +This lesson assumes you've finished the online quick starts, and you've installed the [.NET Core SDK](http://dot.net/core) and [Visual Studio Code](https://code.visualstudio.com/). -Create a directory named **list-quickstart** and run `dotnet new console`. +Create a directory named **list-quickstart**. Make that the current directory and run `dotnet new console`. -Open **program.cs** in your favorite editor, and replace the existing code with the following: +Open **Program.cs** in your favorite editor, and replace the existing code with the following: ```csharp using System; @@ -126,43 +127,52 @@ Save the file and type `dotnet run` to try this latest version. Before you start the next section, let's move the current code into a separate method. That makes it easier to start working with a new example. Rename your `Main` method to `WorkingWithStrings` and write a new `Main` method that calls `WorkingWithStrings`. When you have finished, your code should look like this: ```csharp -static void Main(string[] args) -{ - WorkingWithStrings(); -} +using System; +using System.Collections.Generic; -public static void WorkingWithStrings() +namespace list_quickstart { - var names = new List { "", "Ana", "Felipe" }; - foreach (var name in names) + class Program { - Console.WriteLine($"Hello {name.ToUpper()}!"); - } + static void Main(string[] args) + { + WorkingWithStrings(); + } - Console.WriteLine(); - names.Add("Maria"); - names.Add("Bill"); - names.Remove("Ana"); - foreach (var name in names) - { - Console.WriteLine($"Hello {name.ToUpper()}!"); - } + public static void WorkingWithStrings() + { + var names = new List { "", "Ana", "Felipe" }; + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } - Console.WriteLine($"My name is {names[0]}"); - Console.WriteLine($"I've added {names[2]} and {names[3]} to the list"); + Console.WriteLine(); + names.Add("Maria"); + names.Add("Bill"); + names.Remove("Ana"); + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } - Console.WriteLine($"The list has {names.Count} people in it"); + Console.WriteLine($"My name is {names[0]}"); + Console.WriteLine($"I've added {names[2]} and {names[3]} to the list"); - var index = names.IndexOf("Felipe"); - Console.WriteLine($"The name {names[index]} is at index {index}"); + Console.WriteLine($"The list has {names.Count} people in it"); - var notFound = names.IndexOf("Not Found"); - Console.WriteLine($"When an item is not found, IndexOf returns {notFound}"); + var index = names.IndexOf("Felipe"); + Console.WriteLine($"The name {names[index]} is at index {index}"); - names.Sort(); - foreach (var name in names) - { - Console.WriteLine($"Hello {name.ToUpper()}!"); + var notFound = names.IndexOf("Not Found"); + Console.WriteLine($"When an item is not found, IndexOf returns {notFound}"); + + names.Sort(); + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } + } } } ``` diff --git a/docs/csharp/quick-starts/list-collection.yml b/docs/csharp/quick-starts/list-collection.yml index f65385bfa74c9..ae5273ebdc9b3 100644 --- a/docs/csharp/quick-starts/list-collection.yml +++ b/docs/csharp/quick-starts/list-collection.yml @@ -15,7 +15,7 @@ items: - title: Create lists durationInMinutes: 2 content: | - Run the following code in the interactive window. To do that, type the following code block in the interactive window and click the **Run** button: + Run the following code in the interactive window. To do that, type the following code block in the interactive window (replace "" with your name) and click the **Run** button: ```csharp var names = new List { "", "Ana", "Felipe" }; @@ -38,9 +38,9 @@ items: - title: Modify list contents durationInMinutes: 3 content: | - The collection you created using the type. This type stores sequences of elements. You specify the type of the elements between the angle brackets. + The collection you created uses the type. This type stores sequences of elements. You specify the type of the elements between the angle brackets. - One important aspect of this type is that it can grow or shrink, enabling you to add or remove elements. Add this code below the code you've already written: + One important aspect of this type is that it can grow or shrink, enabling you to add or remove elements. Add the following code below the code you've already written: ```csharp Console.WriteLine(); @@ -55,14 +55,14 @@ items: You've added two more names to the end of the list. You've also removed one as well. - The enables you to reference individual items by **index** as well. You access items using the `[` and `]` tokens. Add this code to the bottom and try it: + The enables you to reference individual items by **index** as well. You access items using the `[` and `]` tokens. Add the following code below what you've already written and try it: ```csharp - Console.WriteLine($"My name is {names[0]}"); - Console.WriteLine($"I've added {names[2]} and {names[3]} to the list"); + Console.WriteLine($"My name is {names[0]}."); + Console.WriteLine($"I've added {names[2]} and {names[3]} to the list."); ``` - You're not allowed to access past the end of the list. You can check how long the list is using the `` property: + You're not allowed to access past the end of the list. You can check how long the list is using the property. Add the following code to try it: ```csharp Console.WriteLine($"The list has {names.Count} people in it"); @@ -76,7 +76,7 @@ items: - title: Search and sort lists durationInMinutes: 5 content: | - Our samples use relatively small lists, but your applications may 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 method searches for an item and returns the index of the item. Try this to see how it works: + Our samples use relatively small lists, but your applications may 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 method searches for an item and returns the index of the item. Try this to see how it works. Add the following code below what you've written so far: ```csharp var index = names.IndexOf("Felipe"); @@ -89,7 +89,7 @@ items: You may not know if an item is in the list, so you should always check the index returned by . If it is -1, the item was not found. - The items in your list can be sorted as well. The method sorts all the items in the list in their normal order (alphabetically in the case of strings): + The items in your list can be sorted as well. The method sorts all the items in the list in their normal order (alphabetically in the case of strings). Add this code and run again: ```csharp names.Sort(); @@ -108,7 +108,7 @@ items: You've been using the `string` type in lists so far. Let's make a using a different type. Let's build a set of numbers. Delete the code you wrote so far, and replace it with this: ```csharp - var fibonacciNumbers = new List {1, 1,}; + var fibonacciNumbers = new List {1, 1}; ``` That creates a list of integers, and sets the first two integers to the value 1. These are the first two values of a *Fibonacci Sequence*, a sequence of numbers. Each next Fibonacci number is found by taking the sum of the previous two numbers. Add this code: @@ -142,7 +142,7 @@ items: Did you come up with something like this? ```csharp - var fibonacciNumbers = new List {1, 1,}; + var fibonacciNumbers = new List {1, 1}; while (fibonacciNumbers.Count < 20) { diff --git a/samples/csharp/list-quickstart/Program.cs b/samples/csharp/list-quickstart/Program.cs index 84d2d77db916b..e7b1687ce5279 100644 --- a/samples/csharp/list-quickstart/Program.cs +++ b/samples/csharp/list-quickstart/Program.cs @@ -12,8 +12,6 @@ static void Main(string[] args) var fibonacciNumbers = new List {1, 1}; - fibonacciNumbers.Add(previous + previous2); - while (fibonacciNumbers.Count < 20) { var previous = fibonacciNumbers[fibonacciNumbers.Count - 1]; From 9b07ee206a375ab6ca7496a62f3576f03aab9031 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 16 Oct 2017 16:36:39 -0400 Subject: [PATCH 8/8] missed one xref in code style --- docs/csharp/quick-starts/arrays-and-collections.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/quick-starts/arrays-and-collections.md b/docs/csharp/quick-starts/arrays-and-collections.md index e03eb628693a5..02babcbb82b36 100644 --- a/docs/csharp/quick-starts/arrays-and-collections.md +++ b/docs/csharp/quick-starts/arrays-and-collections.md @@ -80,7 +80,7 @@ Console.WriteLine($"My name is {names[0]}"); Console.WriteLine($"I've added {names[2]} and {names[3]} to the list"); ``` -You cannot access an index beyond the end of the list. Remember that indices start at 0, so the largest valid index is one less than the number of items in the list. You can check how long the list is using the `` property. Add the following code at the end of the Main method: +You cannot access an index beyond the end of the list. Remember that indices start at 0, so the largest valid index is one less than the number of items in the list. You can check how long the list is using the property. Add the following code at the end of the Main method: ```csharp Console.WriteLine($"The list has {names.Count} people in it");