You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
Hereyou'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.
> [!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.
>Ifyouseeamessagesaying"Couldn't find a project to run", ensurethattheTerminalcommandpromptdisplaystheexpectedTestProjectfolderlocation. Forexample: `C:\Users\someuser\Desktop\csharpprojects\TestProject>`
43
+
44
+
Whenyourunthecode, 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'renowempty.
45
+
46
+
```Output
47
+
Clearing2... count:4
48
+
--
49
+
--
50
+
--B12
51
+
--A13
52
+
53
+
```
31
54
32
55
## Empty string versus null
33
56
@@ -40,20 +63,46 @@ What if you attempt to retrieve the value of an element that was affected by the
Ifyoufocusonthelineofoutput `After: `, youmaythinkthatthevaluestoredin `pallets[0]` isanemptystring. However, theC# Compilerimplicitlyconvertsthenullvaluetoanemptystringfor presentation.
59
108
@@ -62,64 +111,168 @@ If you focus on the line of output `After: `, you may think that the value store
62
111
To prove that the value stored in `pallets[0]` after being cleared is null, you'll modify the code example to call the `ToLower()` methodon `pallets[0]`. Ifit's a string, it should work fine. But if it'snull, itshouldcausethecodetothrowanexception.
1. Saveyourcodefile, andthenuseVisualStudioCodetorunyourcode. Thistime, whenyourunthecode, you'll see a large error message. If you parse through the text, you'llseethefollowingmessage:
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.
Themoralofthestoryisthat `Array.Clear()` willremoveanarrayelement'sreferencetoavalueifoneexists. Tofixthis, youmightcheckfor 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.
79
151
80
152
```csharp
81
-
if (pallets[0] !=null) Console.WriteLine($"After: {pallets[0].ToLower()}");
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'rerecommendedtolookatIntellisenseorMicrosoftDocsfor 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.
97
191
98
192
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.
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
+
Clearing2... count:4
255
+
--
256
+
--
257
+
--B12
258
+
--A13
259
+
260
+
Resizing6... count:6
261
+
--
262
+
--
263
+
--B12
264
+
--A13
265
+
--C01
266
+
--C02
267
+
268
+
Resizing3... count:3
269
+
--
270
+
--
271
+
--B12
272
+
273
+
```
274
+
275
+
Noticethatcalling `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.
123
276
124
277
### Can you remove null elements from an array?
125
278
@@ -131,4 +284,4 @@ Here's a few important ideas that you covered in this unit:
0 commit comments