Skip to content

Commit 8352ee9

Browse files
IndexOutOfRangeException sample: net10.0, MSTest 4.5.2, index-from-end and range examples (#2363)
Retarget both projects to net10.0, drop the unused System.Data.SqlClient reference, move tests to MSTest 4.5.2 (Assert.ThrowsExactly), align the List example strings with the article, convert to top-level statements and file-scoped namespaces, and add IndexFromEndMethod and RangeMethod with tests.
1 parent a7c1666 commit 8352ee9

6 files changed

Lines changed: 246 additions & 214 deletions

File tree

‎exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/IndexOutOfRangeExceptionProject.csproj‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

9-
<ItemGroup>
10-
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
11-
</ItemGroup>
12-
139
</Project>
Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
namespace IndexOutOfRangeExceptionProject
2-
{
3-
internal class Program
4-
{
5-
private static void Main(string[] args)
6-
{
7-
UtilityHelper utilityHelper = new();
8-
9-
//1) Array - Upper Bound - should be less that the array length
10-
utilityHelper.UpperBoundMethod(false);
11-
12-
//2) Array with custom lower bound
13-
utilityHelper.CustomLowerBoundMethod(2, false);
14-
15-
//3) Incorrect Arguments
16-
Console.WriteLine("Please enter a search argument!!!");
17-
var searchArg = Convert.ToInt32(Console.ReadLine());
18-
utilityHelper.ListIndexOfMethod(searchArg, false);
19-
20-
//4) 2 arrays with different dimensions
21-
utilityHelper.AssignFirstArrayElementToSecondArrayMethod(false);
22-
23-
//5) Getting confused between the index and value at that index
24-
utilityHelper.IndexAndValueMethod(false);
25-
26-
//6 -> DataTable example
27-
utilityHelper.DataTableMethod(false);
28-
}
29-
}
30-
}
1+
using IndexOutOfRangeExceptionProject;
2+
3+
UtilityHelper utilityHelper = new();
4+
5+
//1) Array - Upper Bound - should be less than the array length
6+
utilityHelper.UpperBoundMethod(false);
7+
8+
//2) Array with custom lower bound
9+
utilityHelper.CustomLowerBoundMethod(2, false);
10+
11+
//3) Incorrect Arguments
12+
Console.WriteLine("Please enter a search argument: ");
13+
var searchArg = Convert.ToInt32(Console.ReadLine());
14+
utilityHelper.ListIndexOfMethod(searchArg, false);
15+
16+
//4) 2 arrays with different dimensions
17+
utilityHelper.AssignFirstArrayElementToSecondArrayMethod(false);
18+
19+
//5) Getting confused between the index and value at that index
20+
utilityHelper.IndexAndValueMethod(false);
21+
22+
//6) DataTable example
23+
utilityHelper.DataTableMethod(false);
24+
25+
//7) Index from end, ranges and ElementAtOrDefault
26+
utilityHelper.IndexFromEndMethod(false);
27+
utilityHelper.RangeMethod(false);
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
using System.Data;
22

3-
namespace IndexOutOfRangeExceptionProject.Repository
3+
namespace IndexOutOfRangeExceptionProject.Repository;
4+
5+
public class StaticDataRepository
46
{
5-
public class StaticDataRepository
7+
public static DataTable GetGroceries()
68
{
7-
public static DataTable GetGroceries()
9+
var dataTable = new DataTable
810
{
9-
var dataTable = new DataTable
10-
{
11-
TableName = "Groceries"
12-
};
13-
dataTable.Columns.Add("Id", typeof(int));
14-
dataTable.Columns.Add("Name", typeof(string));
15-
dataTable.Columns.Add("Description", typeof(string));
11+
TableName = "Groceries"
12+
};
13+
dataTable.Columns.Add("Id", typeof(int));
14+
dataTable.Columns.Add("Name", typeof(string));
15+
dataTable.Columns.Add("Description", typeof(string));
1616

17-
dataTable.Rows.Add(1, "Macaroni", "Tasty Pasta");
18-
dataTable.Rows.Add(2, "Ramen", "Tasty Noodles");
19-
dataTable.Rows.Add(3, "Figaro Oil", "Olive Oil");
20-
dataTable.Rows.Add(4, "XYZ Lentils", "Nutritious Pulses");
17+
dataTable.Rows.Add(1, "Macaroni", "Tasty Pasta");
18+
dataTable.Rows.Add(2, "Ramen", "Tasty Noodles");
19+
dataTable.Rows.Add(3, "Figaro Oil", "Olive Oil");
20+
dataTable.Rows.Add(4, "XYZ Lentils", "Nutritious Pulses");
2121

22-
return dataTable;
23-
}
22+
return dataTable;
2423
}
25-
}
24+
}
Lines changed: 123 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,173 @@
11
using IndexOutOfRangeExceptionProject.Repository;
22
using System.Data;
33

4-
namespace IndexOutOfRangeExceptionProject
4+
namespace IndexOutOfRangeExceptionProject;
5+
6+
public class UtilityHelper
57
{
6-
public class UtilityHelper
8+
public int[] NumbersArray { get; set; }
9+
public Array CustomLowerBoundArray { get; set; }
10+
public List<int> MultiplesOfFive { get; set; }
11+
public int[] Array1 { get; set; }
12+
public int[] Array2 { get; set; }
13+
public int[] OddNumbersArray { get; set; }
14+
public DataTable Groceries { get; set; }
15+
16+
public UtilityHelper()
717
{
8-
public int[] NumbersArray { get; set; }
9-
public Array CustomLowerBoundArray { get; set; }
10-
public List<int> MultiplesOfFive { get; set; }
11-
public int[] Array1 { get; set; }
12-
public int[] Array2 { get; set; }
13-
public int[] OddNumbersArray { get; set; }
14-
public DataTable Groceries { get; set; }
18+
NumbersArray = new int[] { 1, 2, 3, 4, 5 };
19+
CustomLowerBoundArray = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] { 1 });
20+
MultiplesOfFive = new List<int> { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
21+
Array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
22+
OddNumbersArray = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
23+
Groceries = StaticDataRepository.GetGroceries();
24+
}
1525

16-
public UtilityHelper()
26+
public void UpperBoundMethod(bool forcefullyRaiseException)
27+
{
28+
if (forcefullyRaiseException)
1729
{
18-
NumbersArray = new int[] { 1, 2, 3, 4, 5 };
19-
CustomLowerBoundArray = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] { 1 });
20-
MultiplesOfFive = new List<int> { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
21-
Array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
22-
OddNumbersArray = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
23-
Groceries = StaticDataRepository.GetGroceries();
30+
for (var i = 0; i <= NumbersArray.Length; i++)
31+
{
32+
Console.WriteLine(NumbersArray[i]);
33+
}
2434
}
25-
26-
public void UpperBoundMethod(bool forcefullyRaiseException)
35+
else
2736
{
28-
if (forcefullyRaiseException)
37+
for (var i = 0; i < NumbersArray.Length; i++)
2938
{
30-
for (var i = 0; i <= NumbersArray.Length; i++)
31-
{
32-
Console.WriteLine(NumbersArray[i]);
33-
}
39+
Console.WriteLine(NumbersArray[i]);
3440
}
35-
else
41+
}
42+
}
43+
44+
public void CustomLowerBoundMethod(int input, bool forcefullyRaiseException)
45+
{
46+
if (forcefullyRaiseException)
47+
{
48+
for (var i = 0; i < CustomLowerBoundArray.Length; i++)
3649
{
37-
for (var i = 0; i < NumbersArray.Length; i++)
38-
{
39-
Console.WriteLine(NumbersArray[i]);
40-
}
50+
CustomLowerBoundArray.SetValue(input, i);
51+
input *= 5;
4152
}
4253
}
43-
44-
public void CustomLowerBoundMethod(int input, bool forcefullyRaiseException)
54+
else
4555
{
46-
if (forcefullyRaiseException)
56+
for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++)
4757
{
48-
for (var i = 0; i < CustomLowerBoundArray.Length; i++)
49-
{
50-
CustomLowerBoundArray.SetValue(input, i);
51-
input *= 5;
52-
}
58+
CustomLowerBoundArray.SetValue(input, i);
59+
input *= 5;
5360
}
54-
else
61+
for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++)
5562
{
56-
for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++)
57-
{
58-
CustomLowerBoundArray.SetValue(input, i);
59-
input *= 5;
60-
}
61-
for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++)
62-
{
63-
Console.WriteLine(CustomLowerBoundArray.GetValue(i));
64-
}
63+
Console.WriteLine(CustomLowerBoundArray.GetValue(i));
6564
}
6665
}
66+
}
6767

68-
public void ListIndexOfMethod(int searchArgument, bool forcefullyRaiseException)
69-
{
70-
var startIndex = MultiplesOfFive.IndexOf(searchArgument);
68+
public void ListIndexOfMethod(int searchArgument, bool forcefullyRaiseException)
69+
{
70+
var startIndex = MultiplesOfFive.IndexOf(searchArgument);
7171

72-
if (forcefullyRaiseException)
72+
if (forcefullyRaiseException)
73+
{
74+
Console.WriteLine($"Let's display the multiples of 5 starting at {searchArgument}");
75+
for (var i = startIndex; i < MultiplesOfFive.Count; i++)
7376
{
74-
Console.WriteLine($"Let's display the even numbers greater than {searchArgument}");
75-
for (var i = startIndex; i < MultiplesOfFive.Count; i++)
76-
{
77-
Console.WriteLine(MultiplesOfFive[i]);
78-
}
77+
Console.WriteLine(MultiplesOfFive[i]);
78+
}
79+
}
80+
else
81+
{
82+
if (startIndex < 0)
83+
{
84+
Console.WriteLine($"The number {searchArgument} does not exist in the list.");
7985
}
8086
else
8187
{
82-
if (startIndex < 0)
83-
{
84-
Console.WriteLine($"The number {searchArgument} does not exist in the list.");
85-
}
86-
else
88+
Console.WriteLine($"Let's display the multiples of 5 starting at {searchArgument}");
89+
for (var i = startIndex; i < MultiplesOfFive.Count; i++)
8790
{
88-
Console.WriteLine($"Let's display the even numbers greater than {searchArgument}");
89-
for (var i = startIndex; i < MultiplesOfFive.Count; i++)
90-
{
91-
Console.WriteLine(MultiplesOfFive[i]);
92-
}
91+
Console.WriteLine(MultiplesOfFive[i]);
9392
}
9493
}
9594
}
95+
}
9696

97-
public void AssignFirstArrayElementToSecondArrayMethod(bool forcefullyRaiseException)
97+
public void AssignFirstArrayElementToSecondArrayMethod(bool forcefullyRaiseException)
98+
{
99+
if (forcefullyRaiseException)
98100
{
99-
if (forcefullyRaiseException)
100-
{
101-
Array2 = new int[7];
102-
Array2[Array1.Length - 1] = Array1[Array1.Length - 1];
103-
Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}");
104-
}
105-
else
106-
{
107-
Array2 = new int[Array1.Length];
108-
Array2[Array1.Length - 1] = Array1[Array1.Length - 1];
109-
Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}");
110-
}
101+
Array2 = new int[7];
102+
Array2[Array1.Length - 1] = Array1[Array1.Length - 1];
103+
Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}");
111104
}
105+
else
106+
{
107+
Array2 = new int[Array1.Length];
108+
Array2[Array1.Length - 1] = Array1[Array1.Length - 1];
109+
Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}");
110+
}
111+
}
112112

113-
public void IndexAndValueMethod(bool forcefullyRaiseException)
113+
public void IndexAndValueMethod(bool forcefullyRaiseException)
114+
{
115+
if (forcefullyRaiseException)
114116
{
115-
if (forcefullyRaiseException)
117+
foreach (var number in OddNumbersArray)
116118
{
117-
foreach (var number in OddNumbersArray)
118-
{
119-
Console.WriteLine($"The odd number is: {OddNumbersArray[number]}");
120-
}
119+
Console.WriteLine($"The odd number is: {OddNumbersArray[number]}");
121120
}
122-
else
121+
}
122+
else
123+
{
124+
foreach (var number in OddNumbersArray)
123125
{
124-
foreach (var number in OddNumbersArray)
125-
{
126-
Console.WriteLine($"The odd number is: {number}");
127-
}
126+
Console.WriteLine($"The odd number is: {number}");
128127
}
129128
}
129+
}
130130

131-
public void DataTableMethod(bool forcefullyRaiseException)
131+
public void DataTableMethod(bool forcefullyRaiseException)
132+
{
133+
if (forcefullyRaiseException)
132134
{
133-
if (forcefullyRaiseException)
135+
for (var i = 0; i < Groceries.Rows.Count; i++)
134136
{
135-
for (var i = 0; i < Groceries.Rows.Count; i++)
136-
{
137-
var row = Groceries.Rows[i];
138-
Console.WriteLine($"{row[0]}\t {row[1]}\t {row[2]}\t {row[3]}");
139-
}
137+
var row = Groceries.Rows[i];
138+
Console.WriteLine($"{row[0]}\t {row[1]}\t {row[2]}\t {row[3]}");
140139
}
141-
else
140+
}
141+
else
142+
{
143+
foreach (DataRow grocery in Groceries.Rows)
142144
{
143-
foreach (DataRow grocery in Groceries.Rows)
145+
foreach (var item in grocery.ItemArray)
144146
{
145-
foreach (var item in grocery.ItemArray)
146-
{
147-
Console.WriteLine($"{item}");
148-
}
149-
Console.WriteLine("********************");
147+
Console.WriteLine($"{item}");
150148
}
149+
Console.WriteLine("********************");
151150
}
152151
}
153152
}
154-
}
153+
154+
public void IndexFromEndMethod(bool forcefullyRaiseException)
155+
{
156+
if (forcefullyRaiseException)
157+
{
158+
Console.WriteLine(NumbersArray[^0]);
159+
}
160+
else
161+
{
162+
Console.WriteLine($"The last element is: {NumbersArray[^1]}");
163+
Console.WriteLine($"The last three elements are: {string.Join(", ", NumbersArray[^3..])}");
164+
Console.WriteLine($"The tenth element or the default: {NumbersArray.ElementAtOrDefault(9)}");
165+
}
166+
}
167+
168+
public void RangeMethod(bool forcefullyRaiseException)
169+
{
170+
var range = forcefullyRaiseException ? 3..7 : 3..5;
171+
Console.WriteLine(string.Join(", ", NumbersArray[range]));
172+
}
173+
}

0 commit comments

Comments
 (0)