Skip to content

Commit 34bab2c

Browse files
authored
Update 3-exercise-clear-resize.md
1 parent 710c157 commit 34bab2c

File tree

1 file changed

+207
-54
lines changed

1 file changed

+207
-54
lines changed

learn-pr/wwl-language/csharp-arrays-operations/includes/3-exercise-clear-resize.md

Lines changed: 207 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,47 @@ The `Array.Clear()` method allows you to remove the contents of specific element
1010
1. Delete or use the line comment operator `//` to comment out all of the code from the previous exercises.
1111

1212
1. Update your code in the Visual Studio Code Editor as follows:
13-
```csharp string[] pallets = { "B14", "A11", "B12", "A13" }; Console.WriteLine("");
14-
Array.Clear(pallets, 0, 2); Console.WriteLine($"Clearing 2 ... count: {pallets.Length}"); foreach (var pallet in pallets) { Console.WriteLine($"-- {pallet}"); }
15-
```
13+
14+
```csharp
15+
string[] pallets = { "B14", "A11", "B12", "A13" };
16+
Console.WriteLine("");
17+
18+
Array.Clear(pallets, 0, 2);
19+
Console.WriteLine($"Clearing 2 ... count: {pallets.Length}");
20+
foreach (var pallet in pallets)
21+
{
22+
Console.WriteLine($"-- {pallet}");
23+
}
24+
25+
```
1626

1727
1. Take a minute to focus on the line of code `Array.Clear(pallets, 0, 2);`.
18-
Here you're using the `Array.Clear()` method to clear the values stored in the elements of the `pallets` array starting at index `0` and clearing `2` elements.
28+
29+
Here you're using the `Array.Clear()` method to clear the values stored in the elements of the `pallets` array starting at index `0` and clearing `2` elements.
1930

2031
1. On the Visual Studio Code **File** menu, select **Save**.
21-
The Program.cs file must be saved before building or running the code.
32+
33+
The Program.cs file must be saved before building or running the code.
2234

2335
1. In the EXPLORER panel, to open a Terminal at your TestProject folder location, right-click **TestProject**, and then select **Open in Integrated Terminal**.
24-
A Terminal panel should open, and should include a command prompt showing that the Terminal is open to your TestProject folder location.
36+
37+
A Terminal panel should open, and should include a command prompt showing that the Terminal is open to your TestProject folder location.
2538

2639
1. At the Terminal command prompt, to run your code, type **dotnet run** and then press Enter.
27-
> [!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>`
28-
When you run the code, you'll see that the values stored in the first two elements of the array have been cleared out. In the `Length` property and the `foreach` statement, the elements still exist, but they're now empty.
29-
```Output Clearing 2 ... count: 4 -- -- -- B12 -- A13
30-
```
40+
41+
> [!NOTE]
42+
> 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>`
43+
44+
When you run the code, you'll see that the values stored in the first two elements of the array have been cleared out. In the `Length` property and the `foreach` statement, the elements still exist, but they're now empty.
45+
46+
```Output
47+
Clearing 2 ... count: 4
48+
--
49+
--
50+
-- B12
51+
-- A13
52+
53+
```
3154

3255
## Empty string versus null
3356

@@ -40,20 +63,46 @@ What if you attempt to retrieve the value of an element that was affected by the
4063
Two approaches are needed to determine the value of a cleared element to see how the C# compiler works with a null value.
4164

4265
1. Insert new code lines around the `Array.Clear(pallets, 0, 2);` code line as follows:
43-
```csharp Console.WriteLine($"Before: {pallets[0]}"); Array.Clear(pallets, 0, 2); Console.WriteLine($"After: {pallets[0]}");
44-
```
66+
67+
```csharp
68+
Console.WriteLine($"Before: {pallets[0]}");
69+
Array.Clear(pallets, 0, 2);
70+
Console.WriteLine($"After: {pallets[0]}");
71+
72+
```
4573

4674
1. Verify your code should match the following code listing:
47-
```csharp string[] pallets = { "B14", "A11", "B12", "A13" }; Console.WriteLine("");
48-
Console.WriteLine($"Before: {pallets[0]}"); Array.Clear(pallets, 0, 2); Console.WriteLine($"After: {pallets[0]}");
49-
Console.WriteLine($"Clearing 2 ... count: {pallets.Length}"); foreach (var pallet in pallets) { Console.WriteLine($"-- {pallet}"); }
50-
```
75+
76+
```csharp
77+
string[] pallets = { "B14", "A11", "B12", "A13" };
78+
Console.WriteLine("");
79+
80+
Console.WriteLine($"Before: {pallets[0]}");
81+
Array.Clear(pallets, 0, 2);
82+
Console.WriteLine($"After: {pallets[0]}");
83+
84+
Console.WriteLine($"Clearing 2 ... count: {pallets.Length}");
85+
foreach (var pallet in pallets)
86+
{
87+
Console.WriteLine($"-- {pallet}");
88+
}
89+
90+
```
5191

5292
1. Save your code file, and then use Visual Studio Code to run your code.
5393

5494
You should see the following output:
55-
```Output Before: B14 After: Clearing 2 ... count: 4 -- -- -- B12 -- A13
56-
```
95+
96+
```Output
97+
Before: B14
98+
After:
99+
Clearing 2 ... count: 4
100+
--
101+
--
102+
-- B12
103+
-- A13
104+
105+
```
57106

58107
If you focus on the line of output `After: `, you may think that the value stored in `pallets[0]` is an empty string. However, the C# Compiler implicitly converts the null value to an empty string for presentation.
59108

@@ -62,64 +111,168 @@ If you focus on the line of output `After: `, you may think that the value store
62111
To prove that the value stored in `pallets[0]` after being cleared is null, you'll modify the code example to call the `ToLower()` method on `pallets[0]`. If it's a string, it should work fine. But if it's null, it should cause the code to throw an exception.
63112

64113
1. To call the `ToLower()` method each time you attempt to write `pallets[0]` to the console, update your code as follows::
65-
```csharp Console.WriteLine($"Before: {pallets[0].ToLower()}"); Array.Clear(pallets, 0, 2); Console.WriteLine($"After: {pallets[0].ToLower()}"); ```
114+
115+
```csharp
116+
Console.WriteLine($"Before: {pallets[0].ToLower()}");
117+
Array.Clear(pallets, 0, 2);
118+
Console.WriteLine($"After: {pallets[0].ToLower()}");
119+
```
66120

67121
1. Make sure your code matches the following code listing:
68-
```csharp string[] pallets = { "B14", "A11", "B12", "A13" }; Console.WriteLine("");
69-
Console.WriteLine($"Before: {pallets[0].ToLower()}"); Array.Clear(pallets, 0, 2); Console.WriteLine($"After: {pallets[0].ToLower()}");
70-
Console.WriteLine($"Clearing 2 ... count: {pallets.Length}"); foreach (var pallet in pallets) { Console.WriteLine($"-- {pallet}"); }
71-
```
122+
123+
```csharp
124+
string[] pallets = { "B14", "A11", "B12", "A13" };
125+
Console.WriteLine("");
126+
127+
Console.WriteLine($"Before: {pallets[0].ToLower()}");
128+
Array.Clear(pallets, 0, 2);
129+
Console.WriteLine($"After: {pallets[0].ToLower()}");
130+
131+
Console.WriteLine($"Clearing 2 ... count: {pallets.Length}");
132+
foreach (var pallet in pallets)
133+
{
134+
Console.WriteLine($"-- {pallet}");
135+
}
136+
137+
```
72138

73139
1. Save your code file, and then use Visual Studio Code to run your code. This time, when you run the code, you'll see a large error message. If you parse through the text, you'll see the following message:
74-
```Output System.NullReferenceException: Object reference not set to an instance of an object.
75-
```
76-
This exception is thrown because the attempt to call the method on the contents of the `pallets[0]` element happens before the C# Compiler has a chance to implicitly convert null to an empty string.
77-
The moral of the story is that `Array.Clear()` will remove an array element's reference to a value if one exists. To fix this, you might check for null before attempt to print the value.
78-
To avoid the error, add an `if` statement before accessing an array element that is potentially null.
140+
141+
```Output
142+
System.NullReferenceException: Object reference not set to an instance of an object.
143+
144+
```
145+
146+
This exception is thrown because the attempt to call the method on the contents of the `pallets[0]` element happens before the C# Compiler has a chance to implicitly convert null to an empty string.
147+
148+
The moral of the story is that `Array.Clear()` will remove an array element's reference to a value if one exists. To fix this, you might check for null before attempt to print the value.
149+
150+
To avoid the error, add an `if` statement before accessing an array element that is potentially null.
79151

80152
```csharp
81-
if (pallets[0] != null) Console.WriteLine($"After: {pallets[0].ToLower()}");
153+
if (pallets[0] != null)
154+
Console.WriteLine($"After: {pallets[0].ToLower()}");
82155
```
83156

84157
### Resize the array to add more elements
85158

86159
1. Next, rework the code listing from Step 1 to include code to resize the array. When complete, your code should match the following code listing:
87-
```csharp string[] pallets = { "B14", "A11", "B12", "A13" }; Console.WriteLine("");
88-
Array.Clear(pallets, 0, 2); Console.WriteLine($"Clearing 2 ... count: {pallets.Length}"); foreach (var pallet in pallets) { Console.WriteLine($"-- {pallet}"); }
89-
Console.WriteLine(""); Array.Resize(ref pallets, 6); Console.WriteLine($"Resizing 6 ... count: {pallets.Length}");
90-
pallets[4] = "C01"; pallets[5] = "C02";
91-
foreach (var pallet in pallets) { Console.WriteLine($"-- {pallet}"); }
92-
```
160+
161+
```csharp
162+
string[] pallets = { "B14", "A11", "B12", "A13" };
163+
Console.WriteLine("");
164+
165+
Array.Clear(pallets, 0, 2);
166+
Console.WriteLine($"Clearing 2 ... count: {pallets.Length}");
167+
foreach (var pallet in pallets)
168+
{
169+
Console.WriteLine($"-- {pallet}");
170+
}
171+
172+
Console.WriteLine("");
173+
Array.Resize(ref pallets, 6);
174+
Console.WriteLine($"Resizing 6 ... count: {pallets.Length}");
175+
176+
pallets[4] = "C01";
177+
pallets[5] = "C02";
178+
179+
foreach (var pallet in pallets)
180+
{
181+
Console.WriteLine($"-- {pallet}");
182+
}
183+
184+
```
93185

94186
1. Take a few minutes to focus on the line `Array.Resize(ref pallets, 6);`.
95-
Here, you're calling the `Resize()` method passing in the `pallets` array by reference, using the `ref` keyword. In some cases, methods require you pass arguments by value (the default) or by reference (using the ref keyword). The reasons why this is necessary requires a long and complicated explanation about of how objects are managed in .NET. Unfortunately, that is beyond the scope of this module. When in doubt, you're recommended to look at Intellisense or Microsoft Docs for examples on how to properly call a given method.
96-
In this case, you're resizing the `pallets` array from four elements to `6`. The new elements are added at the end of the current elements. The two new elements will be null until you assign a value to them.
187+
188+
Here, you're calling the `Resize()` method passing in the `pallets` array by reference, using the `ref` keyword. In some cases, methods require you pass arguments by value (the default) or by reference (using the ref keyword). The reasons why this is necessary requires a long and complicated explanation about of how objects are managed in .NET. Unfortunately, that is beyond the scope of this module. When in doubt, you're recommended to look at Intellisense or Microsoft Docs for examples on how to properly call a given method.
189+
190+
In this case, you're resizing the `pallets` array from four elements to `6`. The new elements are added at the end of the current elements. The two new elements will be null until you assign a value to them.
97191

98192
1. Save your code file, and then use Visual Studio Code to run your code. When you run the code, you should see the following output.
99-
```Output Clearing 2 ... count: 4 -- -- -- B12 -- A13
100-
Resizing 6 ... count: 6 -- -- -- B12 -- A13 -- C01 -- C02
101-
```
193+
194+
```Output
195+
Clearing 2 ... count: 4
196+
--
197+
--
198+
-- B12
199+
-- A13
200+
201+
Resizing 6 ... count: 6
202+
--
203+
--
204+
-- B12
205+
-- A13
206+
-- C01
207+
-- C02
208+
209+
```
102210

103211
### Resize the array to remove elements
104212

105213
Conversely, you can remove array elements using `Array.Resize()`.
106214

107215
1. Update your code in the Visual Studio Code Editor as follows:
108-
```csharp string[] pallets = { "B14", "A11", "B12", "A13" }; Console.WriteLine("");
109-
Array.Clear(pallets, 0, 2); Console.WriteLine($"Clearing 2 ... count: {pallets.Length}"); foreach (var pallet in pallets) { Console.WriteLine($"-- {pallet}"); }
110-
Console.WriteLine(""); Array.Resize(ref pallets, 6); Console.WriteLine($"Resizing 6 ... count: {pallets.Length}");
111-
pallets[4] = "C01"; pallets[5] = "C02";
112-
foreach (var pallet in pallets) { Console.WriteLine($"-- {pallet}"); }
113-
Console.WriteLine(""); Array.Resize(ref pallets, 3); Console.WriteLine($"Resizing 3 ... count: {pallets.Length}");
114-
foreach (var pallet in pallets) { Console.WriteLine($"-- {pallet}"); }
115-
```
216+
217+
```csharp
218+
string[] pallets = { "B14", "A11", "B12", "A13" };
219+
Console.WriteLine("");
220+
221+
Array.Clear(pallets, 0, 2);
222+
Console.WriteLine($"Clearing 2 ... count: {pallets.Length}");
223+
foreach (var pallet in pallets)
224+
{
225+
Console.WriteLine($"-- {pallet}");
226+
}
227+
228+
Console.WriteLine("");
229+
Array.Resize(ref pallets, 6);
230+
Console.WriteLine($"Resizing 6 ... count: {pallets.Length}");
231+
232+
pallets[4] = "C01";
233+
pallets[5] = "C02";
234+
235+
foreach (var pallet in pallets)
236+
{
237+
Console.WriteLine($"-- {pallet}");
238+
}
239+
240+
Console.WriteLine("");
241+
Array.Resize(ref pallets, 3);
242+
Console.WriteLine($"Resizing 3 ... count: {pallets.Length}");
243+
244+
foreach (var pallet in pallets)
245+
{
246+
Console.WriteLine($"-- {pallet}");
247+
}
248+
249+
```
116250

117251
1. Save your code file, and then use Visual Studio Code to run your code. When you run the code, you should see the following output:
118-
```Output Clearing 2 ... count: 4 -- -- -- B12 -- A13
119-
Resizing 6 ... count: 6 -- -- -- B12 -- A13 -- C01 -- C02
120-
Resizing 3 ... count: 3 -- -- -- B12
121-
```
122-
Notice that calling `Array.Resize()` didn't eliminate the first two null elements. Rather, it removed the last three elements. Notably, last three elements were removed even though they contained string values.
252+
253+
```Output
254+
Clearing 2 ... count: 4
255+
--
256+
--
257+
-- B12
258+
-- A13
259+
260+
Resizing 6 ... count: 6
261+
--
262+
--
263+
-- B12
264+
-- A13
265+
-- C01
266+
-- C02
267+
268+
Resizing 3 ... count: 3
269+
--
270+
--
271+
-- B12
272+
273+
```
274+
275+
Notice that calling `Array.Resize()` didn't eliminate the first two null elements. Rather, it removed the last three elements. Notably, last three elements were removed even though they contained string values.
123276

124277
### Can you remove null elements from an array?
125278

@@ -131,4 +284,4 @@ Here's a few important ideas that you covered in this unit:
131284

132285
- Use the `Clear()` method to empty the values out of elements in the array.
133286
- Use the `Resize()` method to change the number of elements in the array, removing or adding elements from the end of the array.
134-
- New array elements and cleared elements are null, meaning they don't point to a value in memory.
287+
- New array elements and cleared elements are null, meaning they don't point to a value in memory.

0 commit comments

Comments
 (0)