Add locking to EventWaitHandle.Set/Reset to avoid crashes when another thread dispose...
[mono.git] / mcs / class / corlib / System.Collections / ArrayList.cs
index de65fff020f980e3e0691ea915e4defa0dfef361..0a67c6a2c830e103b621716de866a21453f744e7 100644 (file)
 //
 
 using System.Runtime.InteropServices;
+using System.Diagnostics;
 
 namespace System.Collections 
 {
-#if NET_2_0
-       [ComVisible(true)]
-#endif
        [Serializable]
 #if INSIDE_CORLIB
-       public
+       [ComVisible(true)]
+       [DebuggerDisplay ("Count={Count}")]
+       [DebuggerTypeProxy (typeof (CollectionDebuggerView))]
+       public class ArrayList : IList, ICloneable, ICollection, IEnumerable {
 #else
-       internal
+       internal class ArrayList : IList {
 #endif
-       class ArrayList
-               : IList, ICloneable, ICollection, IEnumerable 
-       {
                #region Enumerator
 
                private sealed class ArrayListEnumerator
                        : IEnumerator, ICloneable 
                {                       
+                       private object m_Current;
+                       private ArrayList m_List;
                        private int m_Pos;
                        private int m_Index;
                        private int m_Count;
-                       private object m_Current;
-                       private ArrayList m_List;
                        private int m_ExpectedStateChanges;
 
                        public ArrayListEnumerator(ArrayList list)
@@ -114,9 +112,9 @@ namespace System.Collections
                sealed class SimpleEnumerator : IEnumerator, ICloneable
                {
                        ArrayList list;
+                       object currentElement;
                        int index;
                        int version;
-                       object currentElement;
                        static object endFlag = new object ();
                                                        
                        public SimpleEnumerator (ArrayList list)
@@ -333,13 +331,13 @@ namespace System.Collections
                        {
                                if (startIndex < 0 || startIndex > m_Adaptee.Count) 
                                {
-                                       throw new ArgumentOutOfRangeException("startIndex", startIndex,
+                                       ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,
                                                "Does not specify valid index.");
                                }
 
                                if (count < 0) 
                                {
-                                       throw new ArgumentOutOfRangeException("count", count,
+                                       ThrowNewArgumentOutOfRangeException ("count", count,
                                                "Can't be less than 0.");
                                }
 
@@ -388,17 +386,17 @@ namespace System.Collections
                        {
                                if (startIndex < 0) 
                                {
-                                       throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
+                                       ThrowNewArgumentOutOfRangeException ("startIndex", startIndex, "< 0");
                                }
 
                                if (count < 0) 
                                {
-                                       throw new ArgumentOutOfRangeException("count", count, "count is negative.");
+                                       ThrowNewArgumentOutOfRangeException ("count", count, "count is negative.");
                                }
 
                                if (startIndex - count  + 1 < 0) 
                                {
-                                       throw new ArgumentOutOfRangeException("count", count, "count is too large.");
+                                       ThrowNewArgumentOutOfRangeException ("count", count, "count is too large.");
                                }
 
                                if (value == null) 
@@ -439,7 +437,7 @@ namespace System.Collections
 
                                if (index > m_Adaptee.Count) 
                                {
-                                       throw new ArgumentOutOfRangeException("index", index,
+                                       ThrowNewArgumentOutOfRangeException ("index", index,
                                                "Index must be >= 0 and <= Count.");
                                }
 
@@ -522,19 +520,19 @@ namespace System.Collections
                        {
                                if (index < 0) 
                                {
-                                       throw new ArgumentOutOfRangeException("index", index,
+                                       ThrowNewArgumentOutOfRangeException ("index", index,
                                                "Can't be less than zero.");
                                }
 
                                if (arrayIndex < 0) 
                                {
-                                       throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex,
+                                       ThrowNewArgumentOutOfRangeException ("arrayIndex", arrayIndex,
                                                "Can't be less than zero.");
                                }
 
                                if (count < 0) 
                                {
-                                       throw new ArgumentOutOfRangeException("index", index,
+                                       ThrowNewArgumentOutOfRangeException ("index", index,
                                                "Can't be less than zero.");
                                }
 
@@ -1795,13 +1793,13 @@ namespace System.Collections
                        {
                                if (startIndex < 0 || startIndex > m_InnerCount) 
                                {
-                                       throw new ArgumentOutOfRangeException("startIndex", startIndex,
+                                       ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,
                                                "Does not specify valid index.");
                                }
 
                                if (count < 0) 
                                {
-                                       throw new ArgumentOutOfRangeException("count", count,
+                                       ThrowNewArgumentOutOfRangeException ("count", count,
                                                "Can't be less than 0.");
                                }
 
@@ -1840,12 +1838,12 @@ namespace System.Collections
                        {
                                if (startIndex < 0) 
                                {
-                                       throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
+                                       ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,  "< 0");
                                }
 
                                if (count < 0) 
                                {
-                                       throw new ArgumentOutOfRangeException("count", count, "count is negative.");
+                                       ThrowNewArgumentOutOfRangeException ("count", count, "count is negative.");
                                }
 
                                int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
@@ -1866,7 +1864,7 @@ namespace System.Collections
                                
                                if (index < 0 || index > m_InnerCount) 
                                {
-                                       throw new ArgumentOutOfRangeException("index", index,
+                                       ThrowNewArgumentOutOfRangeException ("index", index,
                                                "Index must be >= 0 and <= Count.");
                                }
 
@@ -1883,7 +1881,7 @@ namespace System.Collections
 
                                if (index < 0 || index > m_InnerCount) 
                                {
-                                       throw new ArgumentOutOfRangeException("index", index,
+                                       ThrowNewArgumentOutOfRangeException ("index", index,
                                                "Index must be >= 0 and <= Count.");
                                }
 
@@ -1914,7 +1912,7 @@ namespace System.Collections
 
                                if (index < 0 || index > m_InnerCount) 
                                {
-                                       throw new ArgumentOutOfRangeException("index", index,
+                                       ThrowNewArgumentOutOfRangeException ("index", index,
                                                "Index must be >= 0 and <= Count.");
                                }
 
@@ -1959,7 +1957,7 @@ namespace System.Collections
 
                                if (index < 0 || index > m_InnerCount) 
                                {
-                                       throw new ArgumentOutOfRangeException("index", index,
+                                       ThrowNewArgumentOutOfRangeException ("index", index,
                                                "Index must be >= 0 and <= Count.");
                                }
 
@@ -2495,27 +2493,25 @@ namespace System.Collections
 
                #region Fields
 
-#if NET_2_0
                private const int DefaultInitialCapacity = 4;
-#else
-               private const int DefaultInitialCapacity = 16;
-#endif
-               
-               /// <summary>
-               /// Number of items in the list.
-               /// </summary>
-               private int _size;
 
                /// <summary>
                /// Array to store the items.
                /// </summary>
                private object[] _items;
-               
+                               
+               /// <summary>
+               /// Number of items in the list.
+               /// </summary>
+               private int _size;
+
                /// <summary>
                /// Total number of state changes.
                /// </summary>
                private int _version;
 
+               private static readonly object [] EmptyArray = new object [0]; 
+
                #endregion
                
                #region Constructors
@@ -2526,11 +2522,7 @@ namespace System.Collections
                /// </summary>
                public ArrayList()
                {
-#if NET_2_0
-                       _items = new object[0];
-#else
-                       _items = new object[DefaultInitialCapacity];
-#endif
+                       _items = EmptyArray;
                }               
 
                /// <summary>
@@ -2579,7 +2571,7 @@ namespace System.Collections
                {
                        if (capacity < 0) 
                        {
-                               throw new ArgumentOutOfRangeException("capacity",
+                               ThrowNewArgumentOutOfRangeException ("capacity",
                                        capacity, "The initial capacity can't be smaller than zero.");
                        }
 
@@ -2642,7 +2634,7 @@ namespace System.Collections
                        {
                                if (index < 0 || index >= _size) 
                                {
-                                       throw new ArgumentOutOfRangeException("index", index,
+                                       ThrowNewArgumentOutOfRangeException ("index", index,
                                                "Index is less than 0 or more than or equal to the list count.");
                                }
 
@@ -2653,7 +2645,7 @@ namespace System.Collections
                        {
                                if (index < 0 || index >= _size) 
                                {
-                                       throw new ArgumentOutOfRangeException("index", index,
+                                       ThrowNewArgumentOutOfRangeException ("index", index,
                                                "Index is less than 0 or more than or equal to the list count.");
                                }
 
@@ -2697,7 +2689,7 @@ namespace System.Collections
                        {
                                if (value < _size) 
                                {
-                                       throw new ArgumentOutOfRangeException("Capacity", value,
+                                       ThrowNewArgumentOutOfRangeException ("Capacity", value,
                                                "Must be more than count.");
                                }
 
@@ -2888,13 +2880,13 @@ namespace System.Collections
                {
                        if (startIndex < 0 || startIndex > _size) 
                        {
-                               throw new ArgumentOutOfRangeException("startIndex", startIndex,
+                               ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,
                                        "Does not specify valid index.");
                        }
 
                        if (count < 0) 
                        {
-                               throw new ArgumentOutOfRangeException("count", count,
+                               ThrowNewArgumentOutOfRangeException ("count", count,
                                        "Can't be less than 0.");
                        }
 
@@ -2930,7 +2922,7 @@ namespace System.Collections
                {
                        if (index < 0 || index > _size) 
                        {
-                               throw new ArgumentOutOfRangeException("index", index,
+                               ThrowNewArgumentOutOfRangeException ("index", index,
                                        "Index must be >= 0 and <= Count.");
                        }
 
@@ -2952,7 +2944,7 @@ namespace System.Collections
 
                        if (index < 0 || index > _size) 
                        {
-                               throw new ArgumentOutOfRangeException("index", index,
+                               ThrowNewArgumentOutOfRangeException ("index", index,
                                        "Index must be >= 0 and <= Count.");
                        }
 
@@ -3009,7 +3001,7 @@ namespace System.Collections
                {
                        if (index < 0 || index >= _size) 
                        {
-                               throw new ArgumentOutOfRangeException("index", index,
+                               ThrowNewArgumentOutOfRangeException ("index", index,
                                        "Less than 0 or more than list count.");
                        }
 
@@ -3234,12 +3226,12 @@ namespace System.Collections
                {
                        if (index < 0) 
                        {
-                               throw new ArgumentOutOfRangeException("index", index, "Can't be less than 0.");
+                               ThrowNewArgumentOutOfRangeException ("index", index, "Can't be less than 0.");
                        }
 
                        if (count < 0) 
                        {
-                               throw new ArgumentOutOfRangeException("count", count, "Can't be less than 0.");
+                               ThrowNewArgumentOutOfRangeException ("count", count, "Can't be less than 0.");
                        }
 
                        // re-ordered to avoid possible integer overflow
@@ -3249,6 +3241,17 @@ namespace System.Collections
                        }
                }
 
+               internal static void ThrowNewArgumentOutOfRangeException (string name, object actual, string message)
+               {
+                       throw new ArgumentOutOfRangeException (
+#if !INSIDE_CORLIB && NET_2_1
+                               name, message
+#else
+                               name, actual, message
+#endif
+                       );
+               }
+
                public static ArrayList Adapter(IList list) 
                {
                        // LAMESPEC: EWWW.  Other lists aren't *Array*Lists.