From d507b9f286fe4ed13d00c3cd4b9883f4d122203d Mon Sep 17 00:00:00 2001 From: Maria Naggaga Date: Tue, 25 Jun 2019 15:26:35 -0400 Subject: [PATCH] Added beginners tutorial. Added two instructions to Readme --- Beginners/HelloWorld.md | 12 ++ Beginners/Methods.md | 21 +++ Beginners/Readme.md | 13 ++ Beginners/Strings.md | 27 +++ Beginners/TeachTheComputer-formatting.md | 32 ++++ Beginners/TeachTheComputer-repetition-2.md | 41 ++++ Beginners/TeachTheComputer-repetition.md | 29 +++ Beginners/TeachTheComputer.md | 38 ++++ Beginners/myapp/PascalsTriangle.cs | 124 ++++++++++++ Beginners/myapp/Program.cs | 209 +++++++++++++++++++++ Beginners/myapp/myapp.csproj | 13 ++ README.md | 8 +- 12 files changed, 566 insertions(+), 1 deletion(-) create mode 100644 Beginners/HelloWorld.md create mode 100644 Beginners/Methods.md create mode 100644 Beginners/Readme.md create mode 100644 Beginners/Strings.md create mode 100644 Beginners/TeachTheComputer-formatting.md create mode 100644 Beginners/TeachTheComputer-repetition-2.md create mode 100644 Beginners/TeachTheComputer-repetition.md create mode 100644 Beginners/TeachTheComputer.md create mode 100644 Beginners/myapp/PascalsTriangle.cs create mode 100644 Beginners/myapp/Program.cs create mode 100644 Beginners/myapp/myapp.csproj diff --git a/Beginners/HelloWorld.md b/Beginners/HelloWorld.md new file mode 100644 index 0000000..94f6674 --- /dev/null +++ b/Beginners/HelloWorld.md @@ -0,0 +1,12 @@ +# Intro to Programming + +### Run your First Program +Just click the `run` icon to get started + +``` cs --region intro --source-file .\myapp\Program.cs --project .\myapp\myapp.csproj +Console.WriteLine("Hello World!"); +``` +Congratulations! You've run your first C# program. It used the Console.WriteLine method to print `Hello World`. `Console` is a type that represents the console window. `WriteLine` is a method of the `Console` type that prints a line of text. + + +#### Next: [Strings & Variables »](./Strings.md) Previous: [Home «](../README.md) \ No newline at end of file diff --git a/Beginners/Methods.md b/Beginners/Methods.md new file mode 100644 index 0000000..3a16b2a --- /dev/null +++ b/Beginners/Methods.md @@ -0,0 +1,21 @@ +# Learning Methods and Collections + +### Methods +A **method** is a block of code that implements some action. `ToUpper()` is a method you can invoke on a string, like the *name* variable. It will return the same string, converted to uppercase. +``` cs --region methods --source-file .\myapp\Program.cs --project .\myapp\myapp.csproj +var name ="BUILD"; +Console.WriteLine($"Hello {name.ToUpper()}!"); +``` +### Collections +**Collections** hold multiple values of the same type. + +Replace the *name* variable with a *names* variable that has a list of names. Then use a `foreach loop` to iterate over all the names and say hello to each person. + +``` cs --region collections --source-file .\myapp\Program.cs --project .\myapp\myapp.csproj + var names = new List { "Rain", "Sage", "Lee" }; + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } +``` +#### Previous - [Strings & Variables «](./Strings.md) Home - [Home](../README.md) \ No newline at end of file diff --git a/Beginners/Readme.md b/Beginners/Readme.md new file mode 100644 index 0000000..b892616 --- /dev/null +++ b/Beginners/Readme.md @@ -0,0 +1,13 @@ +# Try .NET Enabled Samples +![dotnet try Enabled](https://img.shields.io/badge/Try_.NET-Enabled-501078.svg) + +*Please read the [Try .NET quick setup guide](Setup.md) before starting the tutorial below.* + +

+ +

+

Try .NET Samples

+ +### Table of Content +- [Introduction Programming with C#](HelloWorld.md) +- [Pascals Triangle](TeachTheComputer.md) diff --git a/Beginners/Strings.md b/Beginners/Strings.md new file mode 100644 index 0000000..155143d --- /dev/null +++ b/Beginners/Strings.md @@ -0,0 +1,27 @@ +# Learning Strings + +### Strings +Try modifying the code so that the console says hello to *your name*, instead of the *world*. For example, I used the *Rain*. Replace *Rain* with your name. + +``` cs --region strings --source-file .\myapp\Program.cs --project .\myapp\myapp.csproj +Console.WriteLine("Hello Rain"); +``` +### Variables +In programming we want to store values in a container so we can use them later; these are called `variables`. +Let's store your name in a variable, then read the value from that variable when creating the output message. + +``` cs --region variables --source-file .\myapp\Program.cs --project .\myapp\myapp.csproj +var name = "Rain"; +Console.WriteLine("Hello " + name + "!"); +``` +The first line `var name = "Your Name";` declares a variable, *name* and assigns it a value,`Your Name`. The second line prints out the name. + +### String Interpolation +You've been using `+` to combine variables and constant strings. There is a cleaner way to do this. +Start with including a `$` before the opening quotes of the string. Now, place a variable between curly braces `{ variable name}`, and this will tell C# to replace `variable name` with the value of the variable. This is called **string interpolation**. + +``` cs --region interpolation --source-file .\myapp\Program.cs --project .\myapp\myapp.csproj +var name = "Rain"; +Console.WriteLine($"Hello {name}!"); +``` +#### Next: [Methods »](./Methods.md) Previous: [Hello World «](./HelloWorld.md) Home - [Home](../README.md) diff --git a/Beginners/TeachTheComputer-formatting.md b/Beginners/TeachTheComputer-formatting.md new file mode 100644 index 0000000..4d2f6fc --- /dev/null +++ b/Beginners/TeachTheComputer-formatting.md @@ -0,0 +1,32 @@ +# Let the computer repeat tasks + +Your code should have looked like the following once you finished updating the initialization: + +``` cs --region initialize-in-loop --source-file .\myapp\PascalsTriangle.cs --project .\myapp\myapp.csproj +``` + +That's better. The last step is to improve the formatting. Replace the existing loop to print the rows of the triangle with the following code: + +```csharp +const int fieldWidth = 6; +foreach (int[] row in triangle) +{ + int indent = (5- row.Length) * (fieldWidth / 2); + Console.Write(new string(' ', indent)); + foreach (var item in row) + Console.Write($"{item, fieldWidth}"); + Console.WriteLine(); +} +``` + +This final version makes it easy to build more and more rows. You may notice the number `5` in many places in this code. You can declare a constant instead.: + +```csharp +const int MaxRows = 5; +``` + +Then, replace all instances of `5` with `MaxRows`. Now, if you change `MaxRows`, you'll generate however many rows you want!. Well, within reason. In this environment, your program will time out if go too far. + +You've finished this tutorial. You've taught the computer to do some of your math homework. + +#### Previous: [Build the triangle «](./TeachTheComputer-repetition-2.md) Home: [Home](../README.md) diff --git a/Beginners/TeachTheComputer-repetition-2.md b/Beginners/TeachTheComputer-repetition-2.md new file mode 100644 index 0000000..eaac064 --- /dev/null +++ b/Beginners/TeachTheComputer-repetition-2.md @@ -0,0 +1,41 @@ +# Let the computer repeat tasks + +Your code should have looked like the following once you finished creating the array of rows: + +``` cs --region more-arrays --source-file .\myapp\PascalsTriangle.cs --project .\myapp\myapp.csproj +``` + +That's better. Now, let's update this code to make the computers do the repetitive work instead of you. When you copied and pasted the code for each row, you probably did something like the following: + +1. Paste the previous row. +1. Add 1 to the index of the row. +1. Copy and paste one element in the array, then increase the column indexes. + +Let's make the computer do that for us. Start by removing the statements that initialize the rows of the array. Replace those statements with loop to run for rows 0 through 4: + +```csharp +for (int rowIndex = 0; rowIndex < 5; rowIndex++) +{ +} +``` + +Add code to create a new array for the row. Then set the `1` values for the first and last values: + +```csharp +triangle[rowIndex] = new int[rowIndex + 1]; +triangle[rowIndex][0] = 1; + +triangle[rowIndex][rowIndex] = 1; +``` + +Next, set the columns between the first and last using the previous row. Put the following code where the blank line is in the previous example: + +```csharp +for (int column = 1; column < rowIndex; column++) + triangle[rowIndex][column] = triangle[rowIndex - 1][column - 1] + triangle[rowIndex - 1][column]; +``` + + +Try to modify the sample at the top of the page using these instructions. Then continue to see the answer and improve the formatting. + +#### Next: [Improve formatting »](./TeachTheComputer-formatting.md) Previous: [Less repetition «](./TeachTheComputer-repetition.md) Home: [Home](../README.md) diff --git a/Beginners/TeachTheComputer-repetition.md b/Beginners/TeachTheComputer-repetition.md new file mode 100644 index 0000000..4784592 --- /dev/null +++ b/Beginners/TeachTheComputer-repetition.md @@ -0,0 +1,29 @@ +# Let the computer repeat tasks + +Your hand coded solution should look something like the following code: + +``` cs --region handcoded-answer --source-file .\myapp\PascalsTriangle.cs --project .\myapp\myapp.csproj +``` + +As you put together that solution, you likely found yourself copying, pasting, and modifying some of the code. That is often a great way to explore a problem and try different ways to solve it. Once you understand the problem and have found a good solution, you can improve the code so you don't copy and past similar statements. Instead, let the computer repeat those steps. + +You'll make these changes in two steps. In the first step, you'll make a collection for all the rows in the triangle. Instead of the variables `row0`, `row1`, and so on, declare an array of arrays like the following: + +```csharp +int[][] triangle = new int[5][]; +``` + +From that, you can replace any use of `row0` with `triangle[0]`, `row1` with `triangle[1]` and so on. You'll also remove the declarations of `int[]` before `row0`, `row1` and so on. The variable `triangle` already creates the definitions for those types. As you start replacing variables, you'll see red squiggles highlighting where you use the variables you're replacing. After those replacements, you can replace the code that writes the values with one loop: + +```csharp +foreach (int[] row in triangle) +{ + foreach (var item in row) + Console.Write(item + " "); + Console.WriteLine(); +} +``` + +Try to modify the sample at the top of the page using these instructions. Run when you don't have red squiggles under your code. If the output window turns red, you may have the wrong indices in your array. Once you've got this working, continue for a bit more changes. + +#### Next: [Build the triangle »](./TeachTheComputer-repetition-2.md) Previous: [Teach the computer «](./TeachTheComputer.md) Home: [Home](../README.md) diff --git a/Beginners/TeachTheComputer.md b/Beginners/TeachTheComputer.md new file mode 100644 index 0000000..6e94d56 --- /dev/null +++ b/Beginners/TeachTheComputer.md @@ -0,0 +1,38 @@ +# Teach the computer + +As you learn more about programming, you'll find that you can teach the computer to do more and more work for you. Let's use an example you may recognize from math class. Try this even if math isn't your favorite class. You'll learn to get the computer to do your work for you. + +Pascal's triangle is a visualization of math pattern. The first row is a single `1`. The following rows all have one more item than the previous. Each row is created by adding `1` as the first and last element. Each inner element is found by taking the sum of the two numbers above directly to the left and right. You can see this in the row `1 2 1`. The `2` is the sum of the two `1`s above. Similarly, the `1 3 3 1` row: The first `3` is from the `1 2` in the row above, and next `3` is from the `2 1` that ends the row above. + +```console + 1 + 1 1 + 1 2 1 + 1 3 3 1 + 1 4 6 4 1 + 1 5 10 10 5 1 + 1 6 15 20 15 6 1 + 1 7 21 35 35 21 7 1 + 1 8 28 56 70 56 28 8 1 + 1 9 36 84 126 126 84 36 9 1 + 1 10 45 120 210 252 210 120 45 10 1 + 1 11 55 165 330 462 462 330 165 55 11 1 + 1 12 66 220 495 792 924 792 495 220 66 12 1 + 1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 + 1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 + 1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1 + 1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1 +``` + +Computing successive rows in the triangle is not hard. But, it's very repetitive. Computers are great at tasks like these. You can teach it! + +## Explore code + +A good way to teach the computer is to start by exploring the problem yourself. The first step is to create some simple code that generates the first few rows. Try the following code yourself. It uses some of the concepts you've already learned. This example creates an array for each row of elements. It initializes the elements in the array. The last part prints the values in each row. + +``` cs --region handcoded --source-file .\myapp\PascalsTriangle.cs --project .\myapp\myapp.csproj +``` + +The format of the output doesn't form a nice triangle yet. Let's concentrate on the values first. Try and add a couple more rows to the triangle. Copy and paste the `row2` line and add rows 3 and 4. Do the same with the `foreach` loops that write the values. + +#### Next: [Less repetition »](./TeachTheComputer-repetition.md) Home: [Home](../README.md) diff --git a/Beginners/myapp/PascalsTriangle.cs b/Beginners/myapp/PascalsTriangle.cs new file mode 100644 index 0000000..f636129 --- /dev/null +++ b/Beginners/myapp/PascalsTriangle.cs @@ -0,0 +1,124 @@ +using System; + +namespace myapp +{ + public class PascalsTriangle + { + public static void HardCoded() + { + #region handcoded + int[] row0 = new int[] { 1 }; + int[] row1 = new int[] { 1, 1 }; + int[] row2 = new int[] { 1, row1[0] + row1[1], 1 }; + Console.WriteLine(row0[0]); + + foreach (var item in row1) + Console.Write(item + " "); + Console.WriteLine(); + + foreach (var item in row2) + Console.Write(item + " "); + Console.WriteLine(); + #endregion + } + + public static void MoreHardCoded() + { + #region handcoded-answer + int[] row0 = new int[] { 1 }; + int[] row1 = new int[] { 1, 1 }; + int[] row2 = new int[] { 1, row1[0] + row1[1], 1 }; + int[] row3 = new int[] { 1, row2[0] + row2[1], row2[1] + row2[2], 1 }; + int[] row4 = new int[] { 1, row3[0] + row3[1], row3[1] + row3[2], row3[2] + row3[3], 1 }; + Console.WriteLine(row0[0]); + + foreach (var item in row1) + Console.Write(item + " "); + Console.WriteLine(); + + foreach (var item in row2) + Console.Write(item + " "); + Console.WriteLine(); + + foreach (var item in row3) + Console.Write(item + " "); + Console.WriteLine(); + + foreach (var item in row4) + Console.Write(item + " "); + Console.WriteLine(); + #endregion + } + + public static void ArraysOfArrays() + { + #region more-arrays + int[][] triangle = new int[5][]; + triangle[0] = new int[] { 1 }; + triangle[1] = new int[] { 1, 1 }; + triangle[2] = new int[] { 1, triangle[1][0] + triangle[1][1], 1 }; + triangle[3] = new int[] { 1, triangle[2][0] + triangle[2][1], triangle[2][1] + triangle[2][2], 1 }; + triangle[4] = new int[] { 1, triangle[3][0] + triangle[3][1], triangle[3][1] + triangle[3][2], triangle[3][2] + triangle[3][3], 1 }; + + foreach (int[] row in triangle) + { + foreach (var item in row) + Console.Write(item + " "); + Console.WriteLine(); + } + #endregion + } + + public static void InitInLoops() + { + #region initialize-in-loop + + int[][] triangle = new int[5][]; + for (int rowIndex = 0; rowIndex < 5; rowIndex++) + { + triangle[rowIndex] = new int[rowIndex + 1]; + triangle[rowIndex][0] = 1; + + for (int column = 1; column < rowIndex; column++) + triangle[rowIndex][column] = triangle[rowIndex - 1][column - 1] + triangle[rowIndex - 1][column]; + + triangle[rowIndex][rowIndex] = 1; + } + foreach (int[] row in triangle) + { + foreach (var item in row) + Console.Write(item + " "); + Console.WriteLine(); + } + #endregion + } + + public static void Formattings() + { + #region formatting + const int MaxRows = 5; + int[][] triangle = new int[MaxRows][]; + for (int rowIndex = 0; rowIndex < MaxRows; rowIndex++) + { + triangle[rowIndex] = new int[rowIndex + 1]; + triangle[rowIndex][0] = 1; + + for (int column = 1; column < rowIndex; column++) + triangle[rowIndex][column] = triangle[rowIndex - 1][column - 1] + triangle[rowIndex - 1][column]; + + triangle[rowIndex][rowIndex] = 1; + rowIndex++; + } + const int fieldWidth = 6; + foreach (int[] row in triangle) + { + int indent = (MaxRows - row.Length) * (fieldWidth / 2); + Console.Write(new string(' ', indent)); + foreach (var item in row) + Console.Write($"{item, fieldWidth}"); + Console.WriteLine(); + } + #endregion + } + } +} diff --git a/Beginners/myapp/Program.cs b/Beginners/myapp/Program.cs new file mode 100644 index 0000000..1c71a64 --- /dev/null +++ b/Beginners/myapp/Program.cs @@ -0,0 +1,209 @@ +using System; +using System.Collections.Generic; +using System.Linq; +namespace myapp +{ + class Program + { + static void Main(string region = null, + string session = null, + string package = null, + string project = null, + string[] args = null) + { + switch(region) + { + case "intro": + Intro(); + break; + case "strings": + Strings(); + break; + case "variables": + Variables(); + break; + case "interpolation": + Interpolation(); + break; + case "methods": + Methods(); + break; + case "collections": + Collections(); + break; + case "handcoded": + PascalsTriangle.HardCoded(); + break; + case "handcoded-answer": + PascalsTriangle.MoreHardCoded(); + break; + case "more-arrays": + PascalsTriangle.ArraysOfArrays(); + break; + case "initialize-in-loop": + PascalsTriangle.InitInLoops(); + break; + } + } + public static void Intro() + { + #region intro + Console.WriteLine("Hello World!"); + #endregion + } + public static void Strings() + { + #region strings + Console.WriteLine("Hello Rain"); + #endregion + } + public static void Variables() + { + #region variables + var name = "Rain"; + Console.WriteLine("Hello " + name + "!"); + #endregion + } + + public static void Interpolation() + { + #region interpolation + var name = "Rain"; + Console.WriteLine($"Hello {name}!"); + #endregion + } + public static void Methods() + { + #region methods + var name ="Rain"; + Console.WriteLine($"Hello {name.ToUpper()}!"); + #endregion + } + public static void Collections() + { + #region collections + var names = new List { "Rain", "Sage", "Lee" }; + foreach (var name in names) + { + Console.WriteLine($"Hello {name.ToUpper()}!"); + } + #endregion + } + + public static void PascalsTriangleHardCoded() + { + #region handcoded + int[] row0 = new int[] { 1 }; + int[] row1 = new int[] { 1, 1 }; + int[] row2 = new int[] { 1, row1[0] + row1[1], 1 }; + Console.WriteLine(row0[0]); + + foreach (var item in row1) + Console.Write(item + " "); + Console.WriteLine(); + + foreach (var item in row2) + Console.Write(item + " "); + Console.WriteLine(); + #endregion + } + + public static void PascalsTriangleMoreHardCoded() + { + #region handcoded-answer + int[] row0 = new int[] { 1 }; + int[] row1 = new int[] { 1, 1 }; + int[] row2 = new int[] { 1, row1[0] + row1[1], 1 }; + int[] row3 = new int[] { 1, row2[0] + row2[1], row2[1] + row2[2], 1 }; + int[] row4 = new int[] { 1, row3[0] + row3[1], row3[1] + row3[2], row3[2] + row3[3], 1 }; + Console.WriteLine(row0[0]); + + foreach (var item in row1) + Console.Write(item + " "); + Console.WriteLine(); + + foreach (var item in row2) + Console.Write(item + " "); + Console.WriteLine(); + + foreach (var item in row3) + Console.Write(item + " "); + Console.WriteLine(); + + foreach (var item in row4) + Console.Write(item + " "); + Console.WriteLine(); + #endregion + } + + public static void PascalsTriangleArraysOfArrays() + { + #region more-arrays + int[][] triangle = new int[5][]; + triangle[0] = new int[] { 1 }; + triangle[1] = new int[] { 1, 1 }; + triangle[2] = new int[] { 1, triangle[1][0] + triangle[1][1], 1 }; + triangle[3] = new int[] { 1, triangle[2][0] + triangle[2][1], triangle[2][1] + triangle[2][2], 1 }; + triangle[4] = new int[] { 1, triangle[3][0] + triangle[3][1], triangle[3][1] + triangle[3][2], triangle[3][2] + triangle[3][3], 1 }; + + foreach (int[] row in triangle) + { + foreach (var item in row) + Console.Write(item + " "); + Console.WriteLine(); + } + #endregion + } + + public static void PascalsTriangleInitInLoops() + { + #region initialize-in-loop + int[][] triangle = new int[5][]; + for (int rowIndex = 0; rowIndex < 5; rowIndex++) + { + triangle[rowIndex] = new int[rowIndex + 1]; + triangle[rowIndex][0] = 1; + + for (int column = 1; column < rowIndex; column++) + triangle[rowIndex][column] = triangle[rowIndex - 1][column - 1] + triangle[rowIndex - 1][column]; + + triangle[rowIndex][rowIndex] = 1; + } + foreach (int[] row in triangle) + { + foreach (var item in row) + Console.Write(item + " "); + Console.WriteLine(); + } + #endregion + } + + public static void PascalsTriangleFormattings() + { + #region formatting + const int MaxRows = 5; + int[][] triangle = new int[MaxRows][]; + for (int rowIndex = 0; rowIndex < MaxRows; rowIndex++) + { + triangle[rowIndex] = new int[rowIndex + 1]; + triangle[rowIndex][0] = 1; + + for (int column = 1; column < rowIndex; column++) + triangle[rowIndex][column] = triangle[rowIndex - 1][column - 1] + triangle[rowIndex - 1][column]; + + triangle[rowIndex][rowIndex] = 1; + rowIndex++; + } + const int fieldWidth = 6; + foreach (int[] row in triangle) + { + int indent = (MaxRows - row.Length) * (fieldWidth / 2); + Console.Write(new string(' ', indent)); + foreach (var item in row) + Console.Write($"{item, fieldWidth}"); + Console.WriteLine(); + } + #endregion + } + } +} diff --git a/Beginners/myapp/myapp.csproj b/Beginners/myapp/myapp.csproj new file mode 100644 index 0000000..b282444 --- /dev/null +++ b/Beginners/myapp/myapp.csproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp3.0 + + + + + + + + diff --git a/README.md b/README.md index d40ddf2..66df534 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ [![Build Status](https://dev.azure.com/dnceng/public/_apis/build/status/dotnet/try-samples/try-samples-ci?branchName=master)](https://dev.azure.com/dnceng/public/_build/latest?definitionId=548&branchName=master) +![dotnet try Enabled](https://img.shields.io/badge/Try_.NET-Enabled-501078.svg) # Try .NET dotnet bot in space || [**Basics**](#basics) • [**Contribution Guidelines**](#contribution) || @@ -9,13 +10,18 @@ This repository contains samples and tutorials that run using the [dotnet try](h You can explore these tutorials in your environment using the `dotnet try` global tool: +*Prerequisite: Install .NET Core SDK [3.0](https://dotnet.microsoft.com/download/dotnet-core/3.0)* 1. Install the [dotnet-try](https://github.com/dotnet/try/blob/master/README.md#setup) global tool. 1. Clone this repository. 1. Set the current directory to one of the sample tutorials (or use the current folder to see all samples). 1. Run `dotnet try`. +![dotnet try](https://user-images.githubusercontent.com/2546640/57164943-ab35f080-6dc3-11e9-8230-ee521e00e428.gif) +1. This will launch the browser.Now, you can read the documentation and run code in one place. -The tutorials in this repository are: +![dotnet try - _Readme md (2)](https://user-images.githubusercontent.com/2546640/57165217-737b7880-6dc4-11e9-8b4e-0e70966ac03d.gif) +## The tutorials in this repository are: +- [Beginners Guide to C#](./Beginners/Readme.md) - [Explore C# 7 through C# 7.3](./csharp7/readme.md). - [Explore C# 8](./csharp8/readme.md).