frankcollins3 / fcc-mcsft-cSharp

FreeCodeCamp & Microsoft C# course:
1 stars 0 forks source link

out of bounds for .Length = 4 Arr.Clear(2, 3); [7:18am] #24

Closed frankcollins3 closed 1 year ago

frankcollins3 commented 1 year ago

attempting to do: string[] pallets = { "B14", "A11", "B12", "A13" }; void showPallets() { foreach(string p in pallets) { Console.Write($"check: \t {p} \n "); } } // Array.Clear(pallets, 0, 2);

error: Array.Clear(pallets, 2, 3); Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.

proposed approach: confused how index is out of bounds of array if the array index ends at [3] and so does Array.Clear()

possible improvements: use array properties to gather index, not explicitly define/guess with hard coded int

frankcollins3 commented 1 year ago

string[] pallets = { "B14", "A11", "B12", "A13" }; 👍 Array.Clear(pallets, 0, 1); check: check: A11 check: B12 check: A13

👍 check B14 // gone.

frankcollins3 commented 1 year ago

also proposed approach. not specifying 3 but specifying Array.Length - 1; [7:22am]

frankcollins3 commented 1 year ago

👍 Array.Clear(pallets, 0, pallets.Length -1);

-- A13 check: check: check: 👍 check: A13 [7:24am]