Skip to content

Commit d611b78

Browse files
committed
Convert to file-based-apps
Convert all code from this sample to file-based apps.
1 parent 867a77f commit d611b78

File tree

18 files changed

+70
-130
lines changed

18 files changed

+70
-130
lines changed

docs/csharp/tour-of-csharp/tutorials/branches-and-loops.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ This tutorial teaches you how to write C# code that examines variables and chang
99

1010
TODO: Build an app, create a file.
1111

12-
Open *Program.cs* in your favorite editor, and replace the contents with the following code:
12+
Open *branches-loops.cs* in your favorite editor, and replace the contents with the following code:
1313

14-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="FirstIf":::
14+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="FirstIf":::
1515

1616
Try this code by typing `dotnet run` in your console window. You should see the message "The answer is greater than 10." printed to your console. Modify the declaration of `b` so that the sum is less than 10:
1717

@@ -30,7 +30,7 @@ This first sample shows the power of `if` and Boolean types. A *Boolean* is a va
3030

3131
To execute different code in both the true and false branches, you create an `else` branch that executes when the condition is false. Try an `else` branch. Add the last two lines in the following code snippet (you should already have the first four):
3232

33-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="IfAndElse":::
33+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="IfAndElse":::
3434

3535
The statement following the `else` keyword executes only when the condition being tested is `false`. Combining `if` and `else` with Boolean conditions provides all the power you need to handle both a `true` and a `false` condition.
3636

@@ -39,28 +39,28 @@ The statement following the `else` keyword executes only when the condition bein
3939
4040
Because indentation isn't significant, you need to use `{` and `}` to indicate when you want more than one statement to be part of the block that executes conditionally. C# programmers typically use those braces on all `if` and `else` clauses. The following example is the same as what you created. Modify your code above to match the following code:
4141

42-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="IncludeBraces":::
42+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="IncludeBraces":::
4343

4444
> [!TIP]
4545
> Through the rest of this tutorial, the code samples all include the braces, following accepted practices.
4646
4747
You can test more complicated conditions. Add the following code after the code you've written so far:
4848

49-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="ComplexConditions":::
49+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="ComplexConditions":::
5050

5151
The `==` symbol tests for *equality*. Using `==` distinguishes the test for equality from assignment, which you saw in `a = 5`.
5252

5353
The `&&` represents "and". It means both conditions must be true to execute the statement in the true branch. These examples also show that you can have multiple statements in each conditional branch, provided you enclose them in `{` and `}`.
5454

5555
You can also use `||` to represent "or". Add the following code after what you've written so far:
5656

57-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="UseOr":::
57+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="UseOr":::
5858

5959
Modify the values of `a`, `b`, and `c` and switch between `&&` and `||` to explore. You gain more understanding of how the `&&` and `||` operators work.
6060

6161
You finished the first step. 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. Put the existing code in a method called `ExploreIf()`. Call it from the top of your program. When you finished those changes, your code should look like the following:
6262

63-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="Refactor":::
63+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="Refactor":::
6464

6565
Comment out the call to `ExploreIf()`. It will make the output less cluttered as you work in this section:
6666

@@ -74,7 +74,7 @@ The `//` starts a **comment** in C#. Comments are any text you want to keep in y
7474

7575
Another important concept to create larger programs is **loops**. You use loops to repeat statements that you want executed more than once. Add this code after the call to `ExploreIf`:
7676

77-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="WhileLoop":::
77+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="WhileLoop":::
7878

7979
The `while` statement checks a condition and executes the statement following the `while`. It repeats checking the condition and executing those statements until the condition is false.
8080

@@ -85,7 +85,7 @@ There's one other new operator in this example. The `++` after the `counter` var
8585
8686
The `while` loop tests the condition before executing the code following the `while`. The `do` ... `while` loop executes the code first, and then checks the condition. The *do while* loop is shown in the following code:
8787

88-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="DoLoop":::
88+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="DoLoop":::
8989

9090
This `do` loop and the earlier `while` loop produce the same output.
9191

@@ -95,7 +95,7 @@ Let's move on to one last loop statement.
9595

9696
Another common loop statement that you see in C# code is the `for` loop. Try this code:
9797

98-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="ForLoop":::
98+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="ForLoop":::
9999

100100
The preceding `for` loop does the same work as the `while` loop and the `do` loop you already used. The `for` statement has three parts that control how it works:
101101

@@ -118,15 +118,15 @@ A `while`, `do`, or `for` loop can be nested inside another loop to create a mat
118118

119119
One `for` loop can generate the rows:
120120

121-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="Rows":::
121+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="Rows":::
122122

123123
Another loop can generate the columns:
124124

125-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="Columns":::
125+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="Columns":::
126126

127127
You can nest one loop inside the other to form pairs:
128128

129-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="Nested":::
129+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="Nested":::
130130

131131
You can see that the outer loop increments once for each full run of the inner loop. Reverse the row and column nesting, and see the changes for yourself. When you're done, place the code from this section in a method called `ExploreLoops()`.
132132

@@ -145,7 +145,7 @@ Did you come up with something like this?
145145
<!-- markdownlint-disable MD033 -->
146146
<details>
147147

148-
:::code language="csharp" source="./snippets/BranchesAndLoops/Program.cs" id="Challenge":::
148+
:::code language="csharp" source="./snippets/BranchesAndLoops/branches-loops.cs" id="Challenge":::
149149
</details>
150150
<!-- markdownlint-enable MD033 -->
151151

docs/csharp/tour-of-csharp/tutorials/hello-world.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ TODO: `dotnet new` or create a file.
1313

1414
Type the following code in your new `cs` file:
1515

16-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="HelloWorld":::
16+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="HelloWorld":::
1717

1818
TODO: To run the program, type `dotnet run` (or) `dotnet file.cs` at the command prompt. Congratulations! You ran your first C# program. It's a simple program that prints the message "Hello World!" It used the <xref:System.Console.WriteLine%2A?displayProperty=nameWithType> method to print that message. `Console` is a type that represents the console window. `WriteLine` is a method of the `Console` type that prints a line of text to that text console.
1919

@@ -29,7 +29,7 @@ Your first program printed the `string` "Hello World!" on the screen.
2929
3030
Your first program is limited to printing one message. You can write more useful programs by using *variables*. A *variable* is a symbol you can use to run the same code with different values. Let's try it! Start with the following code:
3131

32-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="Variables":::
32+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="Variables":::
3333

3434
The first line declares a variable, `aFriend`, and assigns it a value, "Bill". The second line prints the name.
3535

@@ -38,13 +38,13 @@ You can assign different values to any variable you declare. You can change the
3838
> [!IMPORTANT]
3939
> Don't delete the declaration of `aFriend`. Add the following code at the end of the preceding code:
4040
41-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="Assignment":::
41+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="Assignment":::
4242

4343
Notice that the same line of code prints two different messages, based on the value stored in the `aFriend` variable.
4444

4545
You might notice that the word "Hello" was missing in the last two messages. Let's fix that now. Modify the lines that print the message to the following code:
4646

47-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="ConcatMessage":::
47+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="ConcatMessage":::
4848

4949
TODO: Run the app again using `dotnet run` (or) `dotnet file.cs` to see the results.
5050

@@ -54,7 +54,7 @@ This process is called [String interpolation](../../language-reference/tokens/in
5454

5555
If you add a `$` before the opening quote of the string, you can then include variables, like `aFriend`, inside the string between curly braces. Give it a try:
5656

57-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="Interpolation":::
57+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="Interpolation":::
5858

5959
TODO: Run the app again using `dotnet run` (or) `dotnet file.cs` to see the results. Instead of "Hello {aFriend}", the message should be "Hello Maira".
6060

@@ -64,11 +64,11 @@ Your last edit was our first look at what you can do with strings. Let's explore
6464

6565
You're not limited to a single variable between the curly braces. Try the following code at the bottom of your app:
6666

67-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="WorkWithStrings":::
67+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="WorkWithStrings":::
6868

6969
Strings are more than a collection of letters. You can find the length of a string using `Length`. `Length` is a **property** of a string and it returns the number of characters in that string. Add the following code at the bottom of your app:
7070

71-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="Properties":::
71+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="Properties":::
7272

7373
> [!TIP]
7474
>
@@ -81,7 +81,7 @@ You've been using a *method*, <xref:System.Console.WriteLine%2A?displayProperty=
8181
Suppose your strings have leading or trailing spaces that you don't want to display. You want to **trim** the spaces from the strings.
8282
The <xref:System.String.Trim%2A> method and related methods <xref:System.String.TrimStart%2A> and <xref:System.String.TrimEnd%2A> do that work. You can just use those methods to remove leading and trailing spaces. Try the following code:
8383

84-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="Trim":::
84+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="Trim":::
8585

8686
The square brackets `[` and `]` help visualize what the `Trim`, `TrimStart,` and, `TrimEnd` methods do. The brackets show where whitespace starts and ends.
8787

@@ -91,17 +91,17 @@ This sample reinforces a couple of important concepts for working with strings.
9191

9292
There are other methods available to work with a string. For example, you probably used a search and replace command in an editor or word processor before. The <xref:System.String.Replace%2A> method does something similar in a string. It searches for a substring and replaces it with different text. The <xref:System.String.Replace%2A> method takes two **parameters**. These parameters are the strings between the parentheses. The first string is the text to search for. The second string is the text to replace it with. Try it for yourself. Add this code. Type it in to see the hints as you start typing `.Re` after the `sayHello` variable:
9393

94-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="Replace":::
94+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="Replace":::
9595

9696
Two other useful methods make a string ALL CAPS or all lower case. Try the following code. Type it in to see how **IntelliSense** provides hints as you start to type `To`:
9797

98-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="UpperLower":::
98+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="UpperLower":::
9999

100100
## Search strings
101101

102102
The other part of a *search and replace* operation is to find text in a string. You can use the <xref:System.String.Contains%2A> method for searching. It tells you if a string contains a substring inside it. Try the following code to explore <xref:System.String.Contains%2A>:
103103

104-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="SearchStrings":::
104+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="SearchStrings":::
105105

106106
The <xref:System.String.Contains%2A> method returns a *boolean* value which tells you if the string you were searching for was found. A *boolean* stores either a `true` or a `false` value. When displayed as text output, they're capitalized: `True` and `False`, respectively. You learn more about *boolean* values in a later lesson.
107107

@@ -120,7 +120,7 @@ Did you come up with something like the following (expand to see the answer):
120120
<!-- markdownlint-disable MD033 -->
121121
<details>
122122

123-
:::code language="csharp" source="./snippets/HelloWorld/Program.cs" id="Challenge":::
123+
:::code language="csharp" source="./snippets/HelloWorld/hello-world.cs" id="Challenge":::
124124
</details>
125125
<!-- markdownlint-enable MD033 -->
126126

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This tutorial teaches you C#. You write C# code and see the results of compiling
1515

1616
Add the following code to your source file. Replace `<name>` with your name. Then, type `dotnet run` to run the code:
1717

18-
:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="BasicList":::
18+
:::code language="csharp" source="./snippets/ListCollection/list.cs" id="BasicList":::
1919

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

@@ -29,17 +29,17 @@ The collection you created uses the <xref:System.Collections.Generic.List%601> t
2929

3030
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):
3131

32-
:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="ModifyList":::
32+
:::code language="csharp" source="./snippets/ListCollection/list.cs" id="ModifyList":::
3333

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

3636
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:
3737

38-
:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="Indexers":::
38+
:::code language="csharp" source="./snippets/ListCollection/list.cs" id="Indexers":::
3939

4040
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:
4141

42-
:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="Property":::
42+
:::code language="csharp" source="./snippets/ListCollection/list.cs" id="Property":::
4343

4444
Type `dotnet run` again to see the results. In C#, indices start at 0, so the largest valid index is one less than the number of items in the list.
4545

@@ -49,23 +49,23 @@ For more information about indices, see the [Explore indexes and ranges](../../t
4949

5050
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:
5151

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

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

5656
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:
5757

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

6060
## Lists of other types
6161

6262
You've been using the `string` type in lists so far. Let's make a <xref:System.Collections.Generic.List%601> using a different type. Let's build a set of numbers. Add the following code at the end of your source file:
6363

64-
:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="CreateList":::
64+
:::code language="csharp" source="./snippets/ListCollection/list.cs" id="CreateList":::
6565

6666
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:
6767

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

7070
Type `dotnet run` to see the results.
7171

@@ -78,7 +78,7 @@ Did you come up with something like this?
7878
<!-- markdownlint-disable MD033 -->
7979
<details>
8080

81-
:::code language="csharp" source="./snippets/ListCollection/Program.cs" id="Answer":::
81+
:::code language="csharp" source="./snippets/ListCollection/list.cs" id="Answer":::
8282

8383
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.
8484
</details>

0 commit comments

Comments
 (0)