Skip to content

Commit 710c157

Browse files
authored
Update 2-exercise-sort-reverse.md
1 parent 7d5c839 commit 710c157

File tree

1 file changed

+94
-29
lines changed

1 file changed

+94
-29
lines changed

learn-pr/wwl-language/csharp-arrays-operations/includes/2-exercise-sort-reverse.md

Lines changed: 94 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,79 +8,144 @@ The `Array` class contains methods that you can use to manipulate the content, a
88
This module includes hands-on activities that guide you through the process of building and running demonstration code. You're encouraged to complete these activities using Visual Studio Code as your development environment. Using Visual Studio Code for these activities will help you to become more comfortable writing and running code in a developer environment that's used by professionals worldwide.
99

1010
1. Open Visual Studio Code.
11-
You can use the Windows Start menu (or equivalent resource for another OS) to open Visual Studio Code.
11+
12+
You can use the Windows Start menu (or equivalent resource for another OS) to open Visual Studio Code.
1213

1314
1. On the Visual Studio Code **File** menu, select **Open Folder**.
1415

1516
1. In the **Open Folder** dialog, navigate to the Windows Desktop folder.
16-
If you have different folder location where you keep code projects, you can use that folder location instead. For this training, the important thing is to have a location that’s easy locate and remember.
17+
18+
If you have different folder location where you keep code projects, you can use that folder location instead. For this training, the important thing is to have a location that’s easy locate and remember.
1719

1820
1. In the **Open Folder** dialog, select **Select Folder**.
19-
If you see a security dialog asking if you trust the authors, select **Yes**.
21+
22+
If you see a security dialog asking if you trust the authors, select **Yes**.
2023

2124
1. On the Visual Studio Code **Terminal** menu, select **New Terminal**.
22-
Notice that a command prompt in the Terminal panel displays the folder path for the current folder. For example:
23-
```dos C:\Users\someuser\Desktop> ```
24-
> [!NOTE] > If working on your own PC, rather than in a sandbox or hosted environment, and you have completed other Microsoft Learn modules in this C# series, you may have already created a project folder for code samples. If that's the case, you can skip over the next step, which is used to create a console app in the TestProject folder.
25+
26+
Notice that a command prompt in the Terminal panel displays the folder path for the current folder. For example:
27+
28+
```dos
29+
C:\Users\someuser\Desktop>
30+
```
31+
32+
> [!NOTE]
33+
> If working on your own PC, rather than in a sandbox or hosted environment, and you have completed other Microsoft Learn modules in this C# series, you may have already created a project folder for code samples. If that's the case, you can skip over the next step, which is used to create a console app in the TestProject folder.
2534
2635
1. At the Terminal command prompt, to create a new console application in a specified folder, type **dotnet new console -o ./CsharpProjects/TestProject** and then press Enter.
27-
This .NET CLI command uses a .NET program template to create a new C# console application project in the specified folder location. The command creates the CsharpProjects and TestProject folders for you, and uses TestProject as the name of the `.csproj` file.
36+
37+
This .NET CLI command uses a .NET program template to create a new C# console application project in the specified folder location. The command creates the CsharpProjects and TestProject folders for you, and uses TestProject as the name of the `.csproj` file.
2838
2939
1. In the EXPLORER panel, expand the **CsharpProjects** folder.
30-
You should see the TestProject folder and two files, a C# program file named Program.cs and a C# project file named TestProject.csproj.
40+
41+
You should see the TestProject folder and two files, a C# program file named Program.cs and a C# project file named TestProject.csproj.
3142
3243
1. In the EXPLORER panel, to view your code file in the Editor panel, select **Program.cs**.
3344
3445
1. Delete the existing code lines.
35-
You'll be using this C# console project to create, build, and run code samples during this module.
46+
47+
You'll be using this C# console project to create, build, and run code samples during this module.
3648
3749
1. Close the Terminal panel.
3850
3951
### Create an array of pallets, then sort them
4052
4153
1. Ensure that you have Visual Studio Code open and Program.cs displayed in the Editor panel.
42-
> [!NOTE] > Program.cs should be empty. If if isn't, select and delete all code lines.
54+
55+
> [!NOTE]
56+
> Program.cs should be empty. If if isn't, select and delete all code lines.
4357
4458
1. Type the following code into the Visual Studio Code Editor:
45-
```csharp string[] pallets = { "B14", "A11", "B12", "A13" };
46-
Console.WriteLine("Sorted..."); Array.Sort(pallets); foreach (var pallet in pallets) { Console.WriteLine($"-- {pallet}"); }
47-
```
59+
60+
```csharp
61+
string[] pallets = { "B14", "A11", "B12", "A13" };
62+
63+
Console.WriteLine("Sorted...");
64+
Array.Sort(pallets);
65+
foreach (var pallet in pallets)
66+
{
67+
Console.WriteLine($"-- {pallet}");
68+
}
69+
70+
```
4871
4972
1. Take a minute to review the `Array.Sort(pallets);` line from the previous code you added.
50-
Here you're using the `Sort()` method of the `Array` class to sort the items in the array alphanumerically.
73+
74+
Here you're using the `Sort()` method of the `Array` class to sort the items in the array alphanumerically.
5175
5276
1. On the Visual Studio Code **File** menu, select **Save**.
53-
The Program.cs file must be saved before building or running the code.
77+
78+
The Program.cs file must be saved before building or running the code.
5479
5580
1. In the EXPLORER panel, to open a Terminal at your TestProject folder location, right-click **TestProject**, and then select **Open in Integrated Terminal**.
56-
A Terminal panel should open, and should include a command prompt showing that the Terminal is open to your TestProject folder location.
81+
82+
A Terminal panel should open, and should include a command prompt showing that the Terminal is open to your TestProject folder location.
5783
5884
1. At the Terminal command prompt, to run your code, type **dotnet run** and then press Enter.
59-
> [!NOTE] > If you see a message saying "Couldn't find a project to run", ensure that the Terminal command prompt displays the expected TestProject folder location. For example: `C:\Users\someuser\Desktop\csharpprojects\TestProject>`
60-
You should see the following output:
61-
```Output Sorted... -- A11 -- A13 -- B12 -- B14
62-
```
85+
86+
> [!NOTE]
87+
> If you see a message saying "Couldn't find a project to run", ensure that the Terminal command prompt displays the expected TestProject folder location. For example: `C:\Users\someuser\Desktop\csharpprojects\TestProject>`
88+
89+
You should see the following output:
90+
91+
```Output
92+
Sorted...
93+
-- A11
94+
-- A13
95+
-- B12
96+
-- B14
97+
98+
```
6399
64100
### Reverse the order of the pallets
65101
66102
1. To reverse the order of the pallets using the `Array.Reverse()` method, update your code as follows:
67-
```csharp string[] pallets = { "B14", "A11", "B12", "A13" };
68-
Console.WriteLine("Sorted..."); Array.Sort(pallets); foreach (var pallet in pallets) { Console.WriteLine($"-- {pallet}"); }
69-
Console.WriteLine(""); Console.WriteLine("Reversed..."); Array.Reverse(pallets); foreach (var pallet in pallets) { Console.WriteLine($"-- {pallet}"); }
70-
```
103+
104+
```csharp
105+
string[] pallets = { "B14", "A11", "B12", "A13" };
106+
107+
Console.WriteLine("Sorted...");
108+
Array.Sort(pallets);
109+
foreach (var pallet in pallets)
110+
{
111+
Console.WriteLine($"-- {pallet}");
112+
}
113+
114+
Console.WriteLine("");
115+
Console.WriteLine("Reversed...");
116+
Array.Reverse(pallets);
117+
foreach (var pallet in pallets)
118+
{
119+
Console.WriteLine($"-- {pallet}");
120+
}
121+
122+
```
71123
72124
1. Focus on the line of code `Array.Reverse(pallets);` line from the previous code you added.
73-
Here, you're using the `Reverse()` method of the `Array` class to reverse the order of items.
125+
126+
Here, you're using the `Reverse()` method of the `Array` class to reverse the order of items.
74127
75128
1. Save your code file, and then use Visual Studio Code to run your code. You should see the following output:
76-
```Output Sorted... -- A11 -- A13 -- B12 -- B14
77-
Reversed... -- B14 -- B12 -- A13 -- A11
78-
```
129+
130+
```Output
131+
Sorted...
132+
-- A11
133+
-- A13
134+
-- B12
135+
-- B14
136+
137+
Reversed...
138+
-- B14
139+
-- B12
140+
-- A13
141+
-- A11
142+
143+
```
79144
80145
## Recap
81146
82147
Here's a few important ideas that you covered in this unit:
83148
84149
- The Array class has methods that can manipulate the size and contents of an array.
85150
- Use the `Sort()` method to manipulate the order based on the given data type of the array.
86-
- Use the `Reverse()` method to flip the order of the elements in the array.
151+
- Use the `Reverse()` method to flip the order of the elements in the array.

0 commit comments

Comments
 (0)