11using IndexOutOfRangeExceptionProject . Repository ;
22using 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