这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions Beginners/HelloWorld.md
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions Beginners/Methods.md
Original file line number Diff line number Diff line change
@@ -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<string> { "Rain", "Sage", "Lee" };
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}
```
#### Previous - [Strings & Variables &laquo;](./Strings.md) Home - [Home](../README.md)
13 changes: 13 additions & 0 deletions Beginners/Readme.md
Original file line number Diff line number Diff line change
@@ -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.*

<p align ="center">
<img src ="https://user-images.githubusercontent.com/2546640/56708992-deee8780-66ec-11e9-9991-eb85abb1d10a.png" width="350">
</p>
<h1 align ="center">Try .NET Samples</h1>

### Table of Content
- [Introduction Programming with C#](HelloWorld.md)
- [Pascals Triangle](TeachTheComputer.md)
27 changes: 27 additions & 0 deletions Beginners/Strings.md
Original file line number Diff line number Diff line change
@@ -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 &raquo;](./Methods.md) Previous: [Hello World &laquo;](./HelloWorld.md) Home - [Home](../README.md)
32 changes: 32 additions & 0 deletions Beginners/TeachTheComputer-formatting.md
Original file line number Diff line number Diff line change
@@ -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 &laquo;](./TeachTheComputer-repetition-2.md) Home: [Home](../README.md)
41 changes: 41 additions & 0 deletions Beginners/TeachTheComputer-repetition-2.md
Original file line number Diff line number Diff line change
@@ -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 &raquo;](./TeachTheComputer-formatting.md) Previous: [Less repetition &laquo;](./TeachTheComputer-repetition.md) Home: [Home](../README.md)
29 changes: 29 additions & 0 deletions Beginners/TeachTheComputer-repetition.md
Original file line number Diff line number Diff line change
@@ -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 &raquo;](./TeachTheComputer-repetition-2.md) Previous: [Teach the computer &laquo;](./TeachTheComputer.md) Home: [Home](../README.md)
38 changes: 38 additions & 0 deletions Beginners/TeachTheComputer.md
Original file line number Diff line number Diff line change
@@ -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 &raquo;](./TeachTheComputer-repetition.md) Home: [Home](../README.md)
124 changes: 124 additions & 0 deletions Beginners/myapp/PascalsTriangle.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Loading