Skip to content

Commit 58083d0

Browse files
authored
Improvements in ArrayList (#191)
***NO_CI***
1 parent ac71099 commit 58083d0

File tree

1 file changed

+71
-46
lines changed

1 file changed

+71
-46
lines changed

nanoFramework.CoreLibrary/System/Collections/ArrayList.cs

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ namespace System.Collections
1818
#endif // NANOCLR_REFLECTION
1919
public class ArrayList : IList, ICloneable
2020
{
21-
private Object[] _items;
21+
private object[] _items;
2222
private int _size;
23+
private object _syncRoot;
2324

2425
// Keep in-sync with c_DefaultCapacity in CLR_RT_HeapBlock_ArrayList in NANOCLR_Runtime__HeapBlock.h
2526
private const int DefaultCapacity = 4;
@@ -29,7 +30,7 @@ public class ArrayList : IList, ICloneable
2930
/// </summary>
3031
public ArrayList()
3132
{
32-
_items = new Object[DefaultCapacity];
33+
_items = new object[DefaultCapacity];
3334
}
3435

3536
/// <summary>
@@ -56,61 +57,54 @@ public virtual int Capacity
5657
/// <value>
5758
/// The number of elements actually contained in the <see cref="ArrayList"/>.
5859
/// </value>
59-
public virtual int Count
60-
{
61-
get { return _size; }
62-
}
60+
public virtual int Count => _size;
6361

6462
/// <summary>
6563
/// Gets a value indicating whether the <see cref="ArrayList"/> has a fixed size.
6664
/// </summary>
6765
/// <value>
6866
/// true if the <see cref="ArrayList"/> has a fixed size; otherwise, false. The default is false.
6967
/// </value>
70-
public virtual bool IsFixedSize
71-
{
72-
get { return false; }
73-
}
68+
public virtual bool IsFixedSize => false;
7469

7570
/// <summary>
7671
/// Gets a value indicating whether the <see cref="ArrayList"/> is read-only.
7772
/// </summary>
7873
/// <value>
7974
/// true if the <see cref="ArrayList"/> is read-only; otherwise, false. The default is false.
8075
/// </value>
81-
public virtual bool IsReadOnly
82-
{
83-
get { return false; }
84-
}
76+
public virtual bool IsReadOnly => false;
8577

8678
/// <summary>
8779
/// Gets a value indicating whether access to the <see cref="ArrayList"/> is synchronized (thread safe).
8880
/// </summary>
8981
/// <value>
9082
/// true if access to the <see cref="ArrayList"/> is synchronized (thread safe); otherwise, false. The default is false.
9183
/// </value>
92-
public virtual bool IsSynchronized
93-
{
94-
get { return false; }
95-
}
84+
public virtual bool IsSynchronized => false;
9685

9786
/// <summary>
9887
/// Gets an object that can be used to synchronize access to the <see cref="ArrayList"/>.
9988
/// </summary>
10089
/// <value>
10190
/// An object that can be used to synchronize access to the <see cref="ArrayList"/>.
10291
/// </value>
103-
public virtual Object SyncRoot
92+
public virtual object SyncRoot
10493
{
105-
get { return this; }
94+
get
95+
{
96+
_syncRoot ??= new object();
97+
98+
return _syncRoot;
99+
}
106100
}
107101

108102
/// <summary>
109103
/// Gets or sets the element at the specified index.
110104
/// </summary>
111105
/// <param name="index">The zero-based index of the element to get or set.</param>
112106
/// <returns></returns>
113-
public virtual extern Object this[int index]
107+
public virtual extern object this[int index]
114108
{
115109
[MethodImpl(MethodImplOptions.InternalCall)]
116110
get;
@@ -123,9 +117,9 @@ public virtual extern Object this[int index]
123117
/// Adds an object to the end of the <see cref="ArrayList"/>.
124118
/// </summary>
125119
/// <param name="value"></param>
126-
/// <returns>The <see cref="Object"/> to be added to the end of the <see cref="ArrayList"/>. The value can be <see langword="null"/>.</returns>
120+
/// <returns>The <see cref="object"/> to be added to the end of the <see cref="ArrayList"/>. The value can be <see langword="null"/>.</returns>
127121
[MethodImpl(MethodImplOptions.InternalCall)]
128-
public virtual extern int Add(Object value);
122+
public virtual extern int Add(object value);
129123

130124
/// <summary>
131125
/// Searches the entire sorted <see cref="ArrayList"/> for an element using the specified comparer and returns the zero-based index of the element.
@@ -135,7 +129,7 @@ public virtual extern Object this[int index]
135129
/// <para>-or-</para>
136130
/// <para>nullNothingnullptrunit a <see langword="null"/> reference(Nothing in Visual Basic) to use the IComparable implementation of each element.</para></param>
137131
/// <returns>The zero-based index of value in the sorted <see cref="ArrayList"/>, if value is found; otherwise, a negative number, which is the bitwise complement of the index of the next element that is larger than value or, if there is no larger element, the bitwise complement of Count.</returns>
138-
public virtual int BinarySearch(Object value, IComparer comparer)
132+
public virtual int BinarySearch(object value, IComparer comparer)
139133
{
140134
return Array.BinarySearch(_items, 0, _size, value, comparer);
141135
}
@@ -150,15 +144,15 @@ public virtual int BinarySearch(Object value, IComparer comparer)
150144
/// Creates a shallow copy of the <see cref="ArrayList"/>.
151145
/// </summary>
152146
/// <returns>A shallow copy of the <see cref="ArrayList"/>.</returns>
153-
public virtual Object Clone()
147+
public virtual object Clone()
154148
{
155149
var arrayList = new ArrayList();
156150

157151
if (_size > DefaultCapacity)
158152
{
159153
// only re-allocate a new array if the size isn't what we need.
160154
// otherwise, the one allocated in the constructor will be just fine
161-
arrayList._items = new Object[_size];
155+
arrayList._items = new object[_size];
162156
}
163157

164158
arrayList._size = _size;
@@ -170,8 +164,8 @@ public virtual Object Clone()
170164
/// Determines whether an element is in the <see cref="ArrayList"/>.
171165
/// </summary>
172166
/// <param name="value"></param>
173-
/// <returns>The<see cref="Object"/> to locate in the <see cref="ArrayList"/>.The value can be <see langword="null"/>.</returns>
174-
public virtual bool Contains(Object value)
167+
/// <returns>The<see cref="object"/> to locate in the <see cref="ArrayList"/>.The value can be <see langword="null"/>.</returns>
168+
public virtual bool Contains(object value)
175169
{
176170
return Array.IndexOf(_items, value, 0, _size) >= 0;
177171
}
@@ -182,17 +176,26 @@ public virtual bool Contains(Object value)
182176
/// <param name="array">The one-dimensional Array that is the destination of the elements copied from <see cref="ArrayList"/>. The Array must have zero-based indexing.</param>
183177
public virtual void CopyTo(Array array)
184178
{
185-
CopyTo(array, 0);
179+
CopyTo(
180+
array,
181+
0);
186182
}
187183

188184
/// <summary>
189185
/// Copies the entire <see cref="ArrayList"/> to a compatible one-dimensional Array, starting at the specified index of the target array.
190186
/// </summary>
191187
/// <param name="array">The one-dimensional Array that is the destination of the elements copied from <see cref="ArrayList"/>. The Array must have zero-based indexing. </param>
192188
/// <param name="index">The zero-based index in array at which copying begins. </param>
193-
public virtual void CopyTo(Array array, int index)
189+
public virtual void CopyTo(
190+
Array array,
191+
int index)
194192
{
195-
Array.Copy(_items, 0, array, index, _size);
193+
Array.Copy(
194+
_items,
195+
0,
196+
array,
197+
index,
198+
_size);
196199
}
197200

198201
/// <summary>
@@ -201,17 +204,24 @@ public virtual void CopyTo(Array array, int index)
201204
/// <returns>An IEnumerator for the entire <see cref="ArrayList"/>.</returns>
202205
public virtual IEnumerator GetEnumerator()
203206
{
204-
return new Array.SzArrayEnumerator(_items, 0, _size);
207+
return new Array.SzArrayEnumerator(
208+
_items,
209+
0,
210+
_size);
205211
}
206212

207213
/// <summary>
208214
/// Searches for the specified Object and returns the zero-based index of the first occurrence within the entire <see cref="ArrayList"/>.
209215
/// </summary>
210216
/// <param name="value">The Object to locate in the <see cref="ArrayList"/>. The value can be <see langword="null"/> reference (Nothing in Visual Basic). </param>
211217
/// <returns>The zero-based index of the first occurrence of value within the entire <see cref="ArrayList"/>, if found; otherwise, -1.</returns>
212-
public virtual int IndexOf(Object value)
218+
public virtual int IndexOf(object value)
213219
{
214-
return Array.IndexOf(_items, value, 0, _size);
220+
return Array.IndexOf(
221+
_items,
222+
value,
223+
0,
224+
_size);
215225
}
216226

217227
/// <summary>
@@ -220,9 +230,13 @@ public virtual int IndexOf(Object value)
220230
/// <param name="value">The Object to locate in the <see cref="ArrayList"/>. The value can be <see langword="null"/> reference (Nothing in Visual Basic). </param>
221231
/// <param name="startIndex">The zero-based starting index of the search. 0 (zero) is valid in an empty list.</param>
222232
/// <returns>The zero-based index of the first occurrence of value within the range of elements in the <see cref="ArrayList"/> that extends from startIndex to the last element, if found; otherwise, -1.</returns>
223-
public virtual int IndexOf(Object value, int startIndex)
233+
public virtual int IndexOf(object value, int startIndex)
224234
{
225-
return Array.IndexOf(_items, value, startIndex, _size - startIndex);
235+
return Array.IndexOf(
236+
_items,
237+
value,
238+
startIndex,
239+
_size - startIndex);
226240
}
227241

228242
/// <summary>
@@ -232,26 +246,37 @@ public virtual int IndexOf(Object value, int startIndex)
232246
/// <param name="startIndex">The zero-based starting index of the search. 0 (zero) is valid in an empty list.</param>
233247
/// <param name="count">The number of elements in the section to search. </param>
234248
/// <returns>The zero-based index of the first occurrence of value within the range of elements in the <see cref="ArrayList"/> that starts at startIndex and contains count number of elements, if found; otherwise, -1.</returns>
235-
public virtual int IndexOf(Object value, int startIndex, int count)
249+
public virtual int IndexOf(object value, int startIndex, int count)
236250
{
237-
return Array.IndexOf(_items, value, startIndex, count);
251+
return Array.IndexOf(
252+
_items,
253+
value,
254+
startIndex,
255+
count);
238256
}
239257

240258
/// <summary>
241259
/// Inserts an element into the <see cref="ArrayList"/> at the specified index.
242260
/// </summary>
243261
/// <param name="index">The zero-based index at which <para>value</para> should be inserted.</param>
244-
/// <param name="value">The <see cref="Object"/> to insert. The `value` can be <see langword="null"/>.</param>
262+
/// <param name="value">The <see cref="object"/> to insert. The `value` can be <see langword="null"/>.</param>
245263
[MethodImpl(MethodImplOptions.InternalCall)]
246-
public virtual extern void Insert(int index, Object value);
264+
public virtual extern void Insert(
265+
int index,
266+
object value);
247267

248268
/// <summary>
249269
/// Removes the first occurrence of a specific object from the <see cref="ArrayList"/>.
250270
/// </summary>
251-
/// <param name="value">The <see cref="Object"/> to remove from the <see cref="ArrayList"/>. The value can be <see langword="null"/>.</param>
252-
public virtual void Remove(Object value)
271+
/// <param name="value">The <see cref="object"/> to remove from the <see cref="ArrayList"/>. The value can be <see langword="null"/>.</param>
272+
public virtual void Remove(object value)
253273
{
254-
var index = Array.IndexOf(_items, value, 0, _size);
274+
var index = Array.IndexOf(
275+
_items,
276+
value,
277+
0,
278+
_size);
279+
255280
if (index >= 0)
256281
{
257282
RemoveAt(index);
@@ -268,13 +293,13 @@ public virtual void Remove(Object value)
268293
#if NANOCLR_REFLECTION
269294

270295
/// <summary>
271-
/// Copies the elements of the <see cref="ArrayList"/> to a new <see cref="Object"/> array.
296+
/// Copies the elements of the <see cref="ArrayList"/> to a new <see cref="object"/> array.
272297
/// </summary>
273298
/// <returns>An Object array containing copies of the elements of the <see cref="ArrayList"/>.</returns>
274299
/// <remarks>Available only in mscorlib build with support for System.Reflection.</remarks>
275-
public virtual Object[] ToArray()
300+
public virtual object[] ToArray()
276301
{
277-
return (Object[])ToArray(typeof(object));
302+
return (object[])ToArray(typeof(object));
278303
}
279304

280305
/// <summary>

0 commit comments

Comments
 (0)