关于System.Collections空间
System.Collections命名空间包含可使用的集合类和相关的接口,提供了集合的基本功能。
该命名空间下的.NET非泛型集合类如下所示:
— System.Collections.ArrayList:数组集合类,使用大小可按动态增加的数组实现Ilist接口。
— System.Collections.BitArray:布尔集合类,管理位值的压缩数组,该值为布尔值。
— System.Collections.Queue:队列,表示对象的先进先出集合。
— System.Collections.Stack:堆栈,表示对象的简单的后进先出集合。
— System.Collections.Hashtable:哈希表,表示键/值对的集合,这些键/值对根据键的哈希代码进行组织
— System.Collections.SortedList:排序集合类,表示键/值对的集合,这些键和值按键排序并可按键和索引访问。
该命名空间下的.NET非泛型接口如下所示:
— System.Collections.ICollection:(继承于IEnumerable)定义所有集合的大小,枚举器和同步方法,可以获取集合中项的个数,并能把项复制到一个简单的数组类型中。
— System.Collections.IComparer:比较两个对象的方法
— System.Collections.IList:(继承于IEnumerable 和 ICollection)表示可按照索引单独访问一组对象,提供集合的项列表,并可以访问这些项。
— System.Collections.IDictionary:(继承于IEnumerable 和 ICollection)表示键/值对的集合
— System.Collections.IDictionaryEnumerator:枚举字典的元素
— System.Collections.IEnumerator:支持在集合上进行简单迭代,可以迭代集合中的项。支持在非泛型集合进行简单迭代。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Runtime.InteropServices; namespace System.Collections { /// <summary> /// Supports a simple iteration over a nongeneric collection. /// </summary> /// <filterpriority>1</filterpriority> [ComVisible(true), Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")] public interface IEnumerator { /// <summary> /// Gets the current element in the collection. /// </summary> /// <returns> /// The current element in the collection. /// </returns> /// <exception cref="T:System.InvalidOperationException"> /// The enumerator is positioned before the first element of the collection or after the last element. /// </exception> /// <filterpriority>2</filterpriority> object Current { get; } /// <summary> /// Advances the enumerator to the next element of the collection. /// </summary> /// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. /// </returns> /// <exception cref="T:System.InvalidOperationException"> /// The collection was modified after the enumerator was created. /// </exception> /// <filterpriority>2</filterpriority> bool MoveNext(); /// <summary> /// Sets the enumerator to its initial position, which is before the first element in the collection. /// </summary> /// <exception cref="T:System.InvalidOperationException"> /// The collection was modified after the enumerator was created. /// </exception> /// <filterpriority>2</filterpriority> void Reset(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Runtime.InteropServices; namespace System.Collections { /// <summary> /// Defines size, enumerators, and synchronization methods for all nongeneric collections. /// </summary> /// <filterpriority>1</filterpriority> [ComVisible(true)] public interface ICollection : IEnumerable { /// <summary> /// Gets the number of elements contained in the <see cref="T:System.Collections.ICollection" />. /// </summary> /// <returns> /// The number of elements contained in the <see cref="T:System.Collections.ICollection" />. /// </returns> /// <filterpriority>2</filterpriority> int Count { get; } /// <summary> /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection" />. /// </summary> /// <returns> /// An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection" />. /// </returns> /// <filterpriority>2</filterpriority> object SyncRoot { get; } /// <summary> /// Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection" /> is synchronized (thread safe). /// </summary> /// <returns>true if access to the <see cref="T:System.Collections.ICollection" /> is synchronized (thread safe); otherwise, false. /// </returns> /// <filterpriority>2</filterpriority> bool IsSynchronized { get; } /// <summary> /// Copies the elements of the <see cref="T:System.Collections.ICollection" /> to an <see cref="T:System.Array" />, starting at a particular <see cref="T:System.Array" /> index. /// </summary> /// <param name="array"> /// The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection" />. The <see cref="T:System.Array" /> must have zero-based indexing. /// </param> /// <param name="index"> /// The zero-based index in <paramref name="array" /> at which copying begins. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="array" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="array" /> is multidimensional. /// /// -or- /// <paramref name="index" /> is equal to or greater than the length of <paramref name="array" />. /// /// -or- /// /// The number of elements in the source <see cref="T:System.Collections.ICollection" /> is greater than the available space from <paramref name="index" /> to the end of the destination <paramref name="array" />. /// </exception> /// <exception cref="T:System.ArgumentException"> /// The type of the source <see cref="T:System.Collections.ICollection" /> cannot be cast automatically to the type of the destination <paramref name="array" />. /// </exception> /// <filterpriority>2</filterpriority> void CopyTo(Array array, int index); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Runtime.InteropServices; namespace System.Collections { /// <summary> /// Represents a non-generic collection of objects that can be individually accessed by index. /// </summary> /// <filterpriority>1</filterpriority> [ComVisible(true)] public interface IList : ICollection, IEnumerable { /// <summary> /// Gets or sets the element at the specified index. /// </summary> /// <returns> /// The element at the specified index. /// </returns> /// <param name="index"> /// The zero-based index of the element to get or set. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is not a valid index in the <see cref="T:System.Collections.IList" />. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The property is set and the <see cref="T:System.Collections.IList" /> is read-only. /// </exception> /// <filterpriority>2</filterpriority> object this[int index] { get; set; } /// <summary> /// Gets a value indicating whether the <see cref="T:System.Collections.IList" /> is read-only. /// </summary> /// <returns>true if the <see cref="T:System.Collections.IList" /> is read-only; otherwise, false. /// </returns> /// <filterpriority>2</filterpriority> bool IsReadOnly { get; } /// <summary> /// Gets a value indicating whether the <see cref="T:System.Collections.IList" /> has a fixed size. /// </summary> /// <returns>true if the <see cref="T:System.Collections.IList" /> has a fixed size; otherwise, false. /// </returns> /// <filterpriority>2</filterpriority> bool IsFixedSize { get; } /// <summary> /// Adds an item to the <see cref="T:System.Collections.IList" />. /// </summary> /// <returns> /// The position into which the new element was inserted. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to add to the <see cref="T:System.Collections.IList" />. /// </param> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.IList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.IList" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> int Add(object value); /// <summary> /// Determines whether the <see cref="T:System.Collections.IList" /> contains a specific value. /// </summary> /// <returns>true if the <see cref="T:System.Object" /> is found in the <see cref="T:System.Collections.IList" />; otherwise, false. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to locate in the <see cref="T:System.Collections.IList" />. /// </param> /// <filterpriority>2</filterpriority> bool Contains(object value); /// <summary> /// Removes all items from the <see cref="T:System.Collections.IList" />. /// </summary> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.IList" /> is read-only. /// </exception> /// <filterpriority>2</filterpriority> void Clear(); /// <summary> /// Determines the index of a specific item in the <see cref="T:System.Collections.IList" />. /// </summary> /// <returns> /// The index of <paramref name="value" /> if found in the list; otherwise, -1. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to locate in the <see cref="T:System.Collections.IList" />. /// </param> /// <filterpriority>2</filterpriority> int IndexOf(object value); /// <summary> /// Inserts an item to the <see cref="T:System.Collections.IList" /> at the specified index. /// </summary> /// <param name="index"> /// The zero-based index at which <paramref name="value" /> should be inserted. /// </param> /// <param name="value"> /// The <see cref="T:System.Object" /> to insert into the <see cref="T:System.Collections.IList" />. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is not a valid index in the <see cref="T:System.Collections.IList" />. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.IList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.IList" /> has a fixed size. /// </exception> /// <exception cref="T:System.NullReferenceException"> /// <paramref name="value" /> is null reference in the <see cref="T:System.Collections.IList" />. /// </exception> /// <filterpriority>2</filterpriority> void Insert(int index, object value); /// <summary> /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.IList" />. /// </summary> /// <param name="value"> /// The <see cref="T:System.Object" /> to remove from the <see cref="T:System.Collections.IList" />. /// </param> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.IList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.IList" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> void Remove(object value); /// <summary> /// Removes the <see cref="T:System.Collections.IList" /> item at the specified index. /// </summary> /// <param name="index"> /// The zero-based index of the item to remove. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is not a valid index in the <see cref="T:System.Collections.IList" />. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.IList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.IList" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> void RemoveAt(int index); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Runtime.InteropServices; namespace System.Collections { /// <summary> /// Represents a nongeneric collection of key/value pairs. /// </summary> /// <filterpriority>1</filterpriority> [ComVisible(true)] public interface IDictionary : ICollection, IEnumerable { /// <summary> /// Gets or sets the element with the specified key. /// </summary> /// <returns> /// The element with the specified key. /// </returns> /// <param name="key"> /// The key of the element to get or set. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The property is set and the <see cref="T:System.Collections.IDictionary" /> object is read-only. /// /// -or- /// /// The property is set, <paramref name="key" /> does not exist in the collection, and the <see cref="T:System.Collections.IDictionary" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> object this[object key] { get; set; } /// <summary> /// Gets an <see cref="T:System.Collections.ICollection" /> object containing the keys of the <see cref="T:System.Collections.IDictionary" /> object. /// </summary> /// <returns> /// An <see cref="T:System.Collections.ICollection" /> object containing the keys of the <see cref="T:System.Collections.IDictionary" /> object. /// </returns> /// <filterpriority>2</filterpriority> ICollection Keys { get; } /// <summary> /// Gets an <see cref="T:System.Collections.ICollection" /> object containing the values in the <see cref="T:System.Collections.IDictionary" /> object. /// </summary> /// <returns> /// An <see cref="T:System.Collections.ICollection" /> object containing the values in the <see cref="T:System.Collections.IDictionary" /> object. /// </returns> /// <filterpriority>2</filterpriority> ICollection Values { get; } /// <summary> /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary" /> object is read-only. /// </summary> /// <returns>true if the <see cref="T:System.Collections.IDictionary" /> object is read-only; otherwise, false. /// </returns> /// <filterpriority>2</filterpriority> bool IsReadOnly { get; } /// <summary> /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary" /> object has a fixed size. /// </summary> /// <returns>true if the <see cref="T:System.Collections.IDictionary" /> object has a fixed size; otherwise, false. /// </returns> /// <filterpriority>2</filterpriority> bool IsFixedSize { get; } /// <summary> /// Determines whether the <see cref="T:System.Collections.IDictionary" /> object contains an element with the specified key. /// </summary> /// <returns>true if the <see cref="T:System.Collections.IDictionary" /> contains an element with the key; otherwise, false. /// </returns> /// <param name="key"> /// The key to locate in the <see cref="T:System.Collections.IDictionary" /> object. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <filterpriority>2</filterpriority> bool Contains(object key); /// <summary> /// Adds an element with the provided key and value to the <see cref="T:System.Collections.IDictionary" /> object. /// </summary> /// <param name="key"> /// The <see cref="T:System.Object" /> to use as the key of the element to add. /// </param> /// <param name="value"> /// The <see cref="T:System.Object" /> to use as the value of the element to add. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.ArgumentException"> /// An element with the same key already exists in the <see cref="T:System.Collections.IDictionary" /> object. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.IDictionary" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.IDictionary" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> void Add(object key, object value); /// <summary> /// Removes all elements from the <see cref="T:System.Collections.IDictionary" /> object. /// </summary> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.IDictionary" /> object is read-only. /// </exception> /// <filterpriority>2</filterpriority> void Clear(); /// <summary> /// Returns an <see cref="T:System.Collections.IDictionaryEnumerator" /> object for the <see cref="T:System.Collections.IDictionary" /> object. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IDictionaryEnumerator" /> object for the <see cref="T:System.Collections.IDictionary" /> object. /// </returns> /// <filterpriority>2</filterpriority> IDictionaryEnumerator GetEnumerator(); /// <summary> /// Removes the element with the specified key from the <see cref="T:System.Collections.IDictionary" /> object. /// </summary> /// <param name="key"> /// The key of the element to remove. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.IDictionary" /> object is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.IDictionary" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> void Remove(object key); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Runtime.InteropServices; namespace System.Collections { /// <summary> /// Enumerates the elements of a nongeneric dictionary. /// </summary> /// <filterpriority>2</filterpriority> [ComVisible(true)] public interface IDictionaryEnumerator : IEnumerator { /// <summary> /// Gets the key of the current dictionary entry. /// </summary> /// <returns> /// The key of the current element of the enumeration. /// </returns> /// <exception cref="T:System.InvalidOperationException"> /// The <see cref="T:System.Collections.IDictionaryEnumerator" /> is positioned before the first entry of the dictionary or after the last entry. /// </exception> /// <filterpriority>2</filterpriority> object Key { get; } /// <summary> /// Gets the value of the current dictionary entry. /// </summary> /// <returns> /// The value of the current element of the enumeration. /// </returns> /// <exception cref="T:System.InvalidOperationException"> /// The <see cref="T:System.Collections.IDictionaryEnumerator" /> is positioned before the first entry of the dictionary or after the last entry. /// </exception> /// <filterpriority>2</filterpriority> object Value { get; } /// <summary> /// Gets both the key and the value of the current dictionary entry. /// </summary> /// <returns> /// A <see cref="T:System.Collections.DictionaryEntry" /> containing both the key and the value of the current dictionary entry. /// </returns> /// <exception cref="T:System.InvalidOperationException"> /// The <see cref="T:System.Collections.IDictionaryEnumerator" /> is positioned before the first entry of the dictionary or after the last entry. /// </exception> /// <filterpriority>2</filterpriority> DictionaryEntry Entry { get; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Runtime.InteropServices; namespace System.Collections { /// <summary> /// Exposes a method that compares two objects. /// </summary> /// <filterpriority>1</filterpriority> [ComVisible(true)] public interface IComparer { /// <summary> /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other. /// </summary> /// <returns> /// Value /// /// Condition /// /// Less than zero /// <paramref name="x" /> is less than <paramref name="y" />. /// /// Zero /// <paramref name="x" /> equals <paramref name="y" />. /// /// Greater than zero /// <paramref name="x" /> is greater than <paramref name="y" />. /// </returns> /// <param name="x"> /// The first object to compare. /// </param> /// <param name="y"> /// The second object to compare. /// </param> /// <exception cref="T:System.ArgumentException"> /// Neither <paramref name="x" /> nor <paramref name="y" /> implements the <see cref="T:System.IComparable" /> interface. /// /// -or- /// <paramref name="x" /> and <paramref name="y" /> are of different types and neither one can handle comparisons with the other. /// </exception> /// <filterpriority>2</filterpriority> int Compare(object x, object y); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Runtime.InteropServices; namespace System.Collections { /// <summary> /// Defines methods to support the comparison of objects for equality. /// </summary> [ComVisible(true)] public interface IEqualityComparer { /// <summary> /// Determines whether the specified objects are equal. /// </summary> /// <returns>true if the specified objects are equal; otherwise, false. /// </returns> /// <param name="x"> /// The first object to compare. /// </param> /// <param name="y"> /// The second object to compare. /// </param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="x" /> and <paramref name="y" /> are of different types and neither one can handle comparisons with the other. /// </exception> bool Equals(object x, object y); /// <summary> /// Returns a hash code for the specified object. /// </summary> /// <returns> /// A hash code for the specified object. /// </returns> /// <param name="obj"> /// The <see cref="T:System.Object" /> for which a hash code is to be returned. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// The type of <paramref name="obj" /> is a reference type and <paramref name="obj" /> is null. /// </exception> int GetHashCode(object obj); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Runtime.InteropServices; namespace System.Collections { /// <summary> /// Supplies a hash code for an object, using a custom hash function. /// </summary> /// <filterpriority>2</filterpriority> [Obsolete("Please use IEqualityComparer instead."), ComVisible(true)] public interface IHashCodeProvider { /// <summary> /// Returns a hash code for the specified object. /// </summary> /// <returns> /// A hash code for the specified object. /// </returns> /// <param name="obj"> /// The <see cref="T:System.Object" /> for which a hash code is to be returned. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// The type of <paramref name="obj" /> is a reference type and <paramref name="obj" /> is null. /// </exception> /// <filterpriority>2</filterpriority> int GetHashCode(object obj); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Threading; namespace System.Collections { /// <summary> /// Implements the <see cref="T:System.Collections.IList" /> interface using an array whose size is dynamically increased as required. /// </summary> /// <filterpriority>1</filterpriority> [DebuggerDisplay("Count = {Count}"), DebuggerTypeProxy(typeof(ArrayList.ArrayListDebugView)), ComVisible(true)] [Serializable] public class ArrayList : IList, ICollection, IEnumerable, ICloneable { [Serializable] private class IListWrapper : ArrayList { [Serializable] private sealed class IListWrapperEnumWrapper : IEnumerator, ICloneable { private IEnumerator _en; private int _remaining; private int _initialStartIndex; private int _initialCount; private bool _firstCall; public object Current { get { if (this._firstCall) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted")); } if (this._remaining < 0) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded")); } return this._en.Current; } } private IListWrapperEnumWrapper() { } internal IListWrapperEnumWrapper(ArrayList.IListWrapper listWrapper, int startIndex, int count) { this._en = listWrapper.GetEnumerator(); this._initialStartIndex = startIndex; this._initialCount = count; while (startIndex-- > 0 && this._en.MoveNext()) { } this._remaining = count; this._firstCall = true; } public object Clone() { return new ArrayList.IListWrapper.IListWrapperEnumWrapper { _en = (IEnumerator)((ICloneable)this._en).Clone(), _initialStartIndex = this._initialStartIndex, _initialCount = this._initialCount, _remaining = this._remaining, _firstCall = this._firstCall }; } public bool MoveNext() { if (this._firstCall) { this._firstCall = false; return this._remaining-- > 0 && this._en.MoveNext(); } if (this._remaining < 0) { return false; } bool flag = this._en.MoveNext(); return flag && this._remaining-- > 0; } public void Reset() { this._en.Reset(); int initialStartIndex = this._initialStartIndex; while (initialStartIndex-- > 0 && this._en.MoveNext()) { } this._remaining = this._initialCount; this._firstCall = true; } } private IList _list; public override int Capacity { get { return this._list.Count; } set { if (value < this._list.Count) { throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_SmallCapacity")); } } } public override int Count { get { return this._list.Count; } } public override bool IsReadOnly { get { return this._list.IsReadOnly; } } public override bool IsFixedSize { get { return this._list.IsFixedSize; } } public override bool IsSynchronized { get { return this._list.IsSynchronized; } } public override object this[int index] { get { return this._list[index]; } set { this._list[index] = value; this._version++; } } public override object SyncRoot { get { return this._list.SyncRoot; } } internal IListWrapper(IList list) { this._list = list; this._version = 0; } public override int Add(object obj) { int result = this._list.Add(obj); this._version++; return result; } public override void AddRange(ICollection c) { this.InsertRange(this.Count, c); } public override int BinarySearch(int index, int count, object value, IComparer comparer) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._list.Count - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } if (comparer == null) { comparer = Comparer.Default; } int i = index; int num = index + count - 1; while (i <= num) { int num2 = (i + num) / 2; int num3 = comparer.Compare(value, this._list[num2]); if (num3 == 0) { return num2; } if (num3 < 0) { num = num2 - 1; } else { i = num2 + 1; } } return ~i; } public override void Clear() { if (this._list.IsFixedSize) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } this._list.Clear(); this._version++; } public override object Clone() { return new ArrayList.IListWrapper(this._list); } public override bool Contains(object obj) { return this._list.Contains(obj); } public override void CopyTo(Array array, int index) { this._list.CopyTo(array, index); } public override void CopyTo(int index, Array array, int arrayIndex, int count) { if (array == null) { throw new ArgumentNullException("array"); } if (index < 0 || arrayIndex < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "arrayIndex", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (count < 0) { throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (array.Length - arrayIndex < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } if (this._list.Count - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } if (array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } for (int i = index; i < index + count; i++) { array.SetValue(this._list[i], arrayIndex++); } } public override IEnumerator GetEnumerator() { return this._list.GetEnumerator(); } public override IEnumerator GetEnumerator(int index, int count) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._list.Count - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } return new ArrayList.IListWrapper.IListWrapperEnumWrapper(this, index, count); } public override int IndexOf(object value) { return this._list.IndexOf(value); } public override int IndexOf(object value, int startIndex) { return this.IndexOf(value, startIndex, this._list.Count - startIndex); } public override int IndexOf(object value, int startIndex, int count) { if (startIndex < 0 || startIndex > this._list.Count) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (count < 0 || startIndex > this._list.Count - count) { throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); } int num = startIndex + count; if (value == null) { for (int i = startIndex; i < num; i++) { if (this._list[i] == null) { return i; } } return -1; } for (int j = startIndex; j < num; j++) { if (this._list[j] != null && this._list[j].Equals(value)) { return j; } } return -1; } public override void Insert(int index, object obj) { this._list.Insert(index, obj); this._version++; } public override void InsertRange(int index, ICollection c) { if (c == null) { throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection")); } if (index < 0 || index > this._list.Count) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (c.Count > 0) { ArrayList arrayList = this._list as ArrayList; if (arrayList != null) { arrayList.InsertRange(index, c); } else { IEnumerator enumerator = c.GetEnumerator(); while (enumerator.MoveNext()) { this._list.Insert(index++, enumerator.Current); } } this._version++; } } public override int LastIndexOf(object value) { return this.LastIndexOf(value, this._list.Count - 1, this._list.Count); } public override int LastIndexOf(object value, int startIndex) { return this.LastIndexOf(value, startIndex, startIndex + 1); } public override int LastIndexOf(object value, int startIndex, int count) { if (this._list.Count == 0) { return -1; } if (startIndex < 0 || startIndex >= this._list.Count) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (count < 0 || count > startIndex + 1) { throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); } int num = startIndex - count + 1; if (value == null) { for (int i = startIndex; i >= num; i--) { if (this._list[i] == null) { return i; } } return -1; } for (int j = startIndex; j >= num; j--) { if (this._list[j] != null && this._list[j].Equals(value)) { return j; } } return -1; } public override void Remove(object value) { int num = this.IndexOf(value); if (num >= 0) { this.RemoveAt(num); } } public override void RemoveAt(int index) { this._list.RemoveAt(index); this._version++; } public override void RemoveRange(int index, int count) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._list.Count - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } if (count > 0) { this._version++; } while (count > 0) { this._list.RemoveAt(index); count--; } } public override void Reverse(int index, int count) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._list.Count - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } int i = index; int num = index + count - 1; while (i < num) { object value = this._list[i]; this._list[i++] = this._list[num]; this._list[num--] = value; } this._version++; } public override void SetRange(int index, ICollection c) { if (c == null) { throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection")); } if (index < 0 || index > this._list.Count - c.Count) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (c.Count > 0) { IEnumerator enumerator = c.GetEnumerator(); while (enumerator.MoveNext()) { this._list[index++] = enumerator.Current; } this._version++; } } public override ArrayList GetRange(int index, int count) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._list.Count - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } return new ArrayList.Range(this, index, count); } public override void Sort(int index, int count, IComparer comparer) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._list.Count - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } object[] array = new object[count]; this.CopyTo(index, array, 0, count); Array.Sort(array, 0, count, comparer); for (int i = 0; i < count; i++) { this._list[i + index] = array[i]; } this._version++; } public override object[] ToArray() { object[] array = new object[this.Count]; this._list.CopyTo(array, 0); return array; } public override Array ToArray(Type type) { if (type == null) { throw new ArgumentNullException("type"); } Array array = Array.CreateInstance(type, this._list.Count); this._list.CopyTo(array, 0); return array; } public override void TrimToSize() { } } [Serializable] private class SyncArrayList : ArrayList { private ArrayList _list; private object _root; public override int Capacity { get { object root; Monitor.Enter(root = this._root); int capacity; try { capacity = this._list.Capacity; } finally { Monitor.Exit(root); } return capacity; } set { object root; Monitor.Enter(root = this._root); try { this._list.Capacity = value; } finally { Monitor.Exit(root); } } } public override int Count { get { object root; Monitor.Enter(root = this._root); int count; try { count = this._list.Count; } finally { Monitor.Exit(root); } return count; } } public override bool IsReadOnly { get { return this._list.IsReadOnly; } } public override bool IsFixedSize { get { return this._list.IsFixedSize; } } public override bool IsSynchronized { get { return true; } } public override object this[int index] { get { object root; Monitor.Enter(root = this._root); object result; try { result = this._list[index]; } finally { Monitor.Exit(root); } return result; } set { object root; Monitor.Enter(root = this._root); try { this._list[index] = value; } finally { Monitor.Exit(root); } } } public override object SyncRoot { get { return this._root; } } internal SyncArrayList(ArrayList list) : base(false) { this._list = list; this._root = list.SyncRoot; } public override int Add(object value) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.Add(value); } finally { Monitor.Exit(root); } return result; } public override void AddRange(ICollection c) { object root; Monitor.Enter(root = this._root); try { this._list.AddRange(c); } finally { Monitor.Exit(root); } } public override int BinarySearch(object value) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.BinarySearch(value); } finally { Monitor.Exit(root); } return result; } public override int BinarySearch(object value, IComparer comparer) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.BinarySearch(value, comparer); } finally { Monitor.Exit(root); } return result; } public override int BinarySearch(int index, int count, object value, IComparer comparer) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.BinarySearch(index, count, value, comparer); } finally { Monitor.Exit(root); } return result; } public override void Clear() { object root; Monitor.Enter(root = this._root); try { this._list.Clear(); } finally { Monitor.Exit(root); } } public override object Clone() { object root; Monitor.Enter(root = this._root); object result; try { result = new ArrayList.SyncArrayList((ArrayList)this._list.Clone()); } finally { Monitor.Exit(root); } return result; } public override bool Contains(object item) { object root; Monitor.Enter(root = this._root); bool result; try { result = this._list.Contains(item); } finally { Monitor.Exit(root); } return result; } public override void CopyTo(Array array) { object root; Monitor.Enter(root = this._root); try { this._list.CopyTo(array); } finally { Monitor.Exit(root); } } public override void CopyTo(Array array, int index) { object root; Monitor.Enter(root = this._root); try { this._list.CopyTo(array, index); } finally { Monitor.Exit(root); } } public override void CopyTo(int index, Array array, int arrayIndex, int count) { object root; Monitor.Enter(root = this._root); try { this._list.CopyTo(index, array, arrayIndex, count); } finally { Monitor.Exit(root); } } public override IEnumerator GetEnumerator() { object root; Monitor.Enter(root = this._root); IEnumerator enumerator; try { enumerator = this._list.GetEnumerator(); } finally { Monitor.Exit(root); } return enumerator; } public override IEnumerator GetEnumerator(int index, int count) { object root; Monitor.Enter(root = this._root); IEnumerator enumerator; try { enumerator = this._list.GetEnumerator(index, count); } finally { Monitor.Exit(root); } return enumerator; } public override int IndexOf(object value) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.IndexOf(value); } finally { Monitor.Exit(root); } return result; } public override int IndexOf(object value, int startIndex) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.IndexOf(value, startIndex); } finally { Monitor.Exit(root); } return result; } public override int IndexOf(object value, int startIndex, int count) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.IndexOf(value, startIndex, count); } finally { Monitor.Exit(root); } return result; } public override void Insert(int index, object value) { object root; Monitor.Enter(root = this._root); try { this._list.Insert(index, value); } finally { Monitor.Exit(root); } } public override void InsertRange(int index, ICollection c) { object root; Monitor.Enter(root = this._root); try { this._list.InsertRange(index, c); } finally { Monitor.Exit(root); } } public override int LastIndexOf(object value) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.LastIndexOf(value); } finally { Monitor.Exit(root); } return result; } public override int LastIndexOf(object value, int startIndex) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.LastIndexOf(value, startIndex); } finally { Monitor.Exit(root); } return result; } public override int LastIndexOf(object value, int startIndex, int count) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.LastIndexOf(value, startIndex, count); } finally { Monitor.Exit(root); } return result; } public override void Remove(object value) { object root; Monitor.Enter(root = this._root); try { this._list.Remove(value); } finally { Monitor.Exit(root); } } public override void RemoveAt(int index) { object root; Monitor.Enter(root = this._root); try { this._list.RemoveAt(index); } finally { Monitor.Exit(root); } } public override void RemoveRange(int index, int count) { object root; Monitor.Enter(root = this._root); try { this._list.RemoveRange(index, count); } finally { Monitor.Exit(root); } } public override void Reverse(int index, int count) { object root; Monitor.Enter(root = this._root); try { this._list.Reverse(index, count); } finally { Monitor.Exit(root); } } public override void SetRange(int index, ICollection c) { object root; Monitor.Enter(root = this._root); try { this._list.SetRange(index, c); } finally { Monitor.Exit(root); } } public override ArrayList GetRange(int index, int count) { object root; Monitor.Enter(root = this._root); ArrayList range; try { range = this._list.GetRange(index, count); } finally { Monitor.Exit(root); } return range; } public override void Sort() { object root; Monitor.Enter(root = this._root); try { this._list.Sort(); } finally { Monitor.Exit(root); } } public override void Sort(IComparer comparer) { object root; Monitor.Enter(root = this._root); try { this._list.Sort(comparer); } finally { Monitor.Exit(root); } } public override void Sort(int index, int count, IComparer comparer) { object root; Monitor.Enter(root = this._root); try { this._list.Sort(index, count, comparer); } finally { Monitor.Exit(root); } } public override object[] ToArray() { object root; Monitor.Enter(root = this._root); object[] result; try { result = this._list.ToArray(); } finally { Monitor.Exit(root); } return result; } public override Array ToArray(Type type) { object root; Monitor.Enter(root = this._root); Array result; try { result = this._list.ToArray(type); } finally { Monitor.Exit(root); } return result; } public override void TrimToSize() { object root; Monitor.Enter(root = this._root); try { this._list.TrimToSize(); } finally { Monitor.Exit(root); } } } [Serializable] private class SyncIList : IList, ICollection, IEnumerable { private IList _list; private object _root; public virtual int Count { get { object root; Monitor.Enter(root = this._root); int count; try { count = this._list.Count; } finally { Monitor.Exit(root); } return count; } } public virtual bool IsReadOnly { get { return this._list.IsReadOnly; } } public virtual bool IsFixedSize { get { return this._list.IsFixedSize; } } public virtual bool IsSynchronized { get { return true; } } public virtual object this[int index] { get { object root; Monitor.Enter(root = this._root); object result; try { result = this._list[index]; } finally { Monitor.Exit(root); } return result; } set { object root; Monitor.Enter(root = this._root); try { this._list[index] = value; } finally { Monitor.Exit(root); } } } public virtual object SyncRoot { get { return this._root; } } internal SyncIList(IList list) { this._list = list; this._root = list.SyncRoot; } public virtual int Add(object value) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.Add(value); } finally { Monitor.Exit(root); } return result; } public virtual void Clear() { object root; Monitor.Enter(root = this._root); try { this._list.Clear(); } finally { Monitor.Exit(root); } } public virtual bool Contains(object item) { object root; Monitor.Enter(root = this._root); bool result; try { result = this._list.Contains(item); } finally { Monitor.Exit(root); } return result; } public virtual void CopyTo(Array array, int index) { object root; Monitor.Enter(root = this._root); try { this._list.CopyTo(array, index); } finally { Monitor.Exit(root); } } public virtual IEnumerator GetEnumerator() { object root; Monitor.Enter(root = this._root); IEnumerator enumerator; try { enumerator = this._list.GetEnumerator(); } finally { Monitor.Exit(root); } return enumerator; } public virtual int IndexOf(object value) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.IndexOf(value); } finally { Monitor.Exit(root); } return result; } public virtual void Insert(int index, object value) { object root; Monitor.Enter(root = this._root); try { this._list.Insert(index, value); } finally { Monitor.Exit(root); } } public virtual void Remove(object value) { object root; Monitor.Enter(root = this._root); try { this._list.Remove(value); } finally { Monitor.Exit(root); } } public virtual void RemoveAt(int index) { object root; Monitor.Enter(root = this._root); try { this._list.RemoveAt(index); } finally { Monitor.Exit(root); } } } [Serializable] private class FixedSizeList : IList, ICollection, IEnumerable { private IList _list; public virtual int Count { get { return this._list.Count; } } public virtual bool IsReadOnly { get { return this._list.IsReadOnly; } } public virtual bool IsFixedSize { get { return true; } } public virtual bool IsSynchronized { get { return this._list.IsSynchronized; } } public virtual object this[int index] { get { return this._list[index]; } set { this._list[index] = value; } } public virtual object SyncRoot { get { return this._list.SyncRoot; } } internal FixedSizeList(IList l) { this._list = l; } public virtual int Add(object obj) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public virtual void Clear() { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public virtual bool Contains(object obj) { return this._list.Contains(obj); } public virtual void CopyTo(Array array, int index) { this._list.CopyTo(array, index); } public virtual IEnumerator GetEnumerator() { return this._list.GetEnumerator(); } public virtual int IndexOf(object value) { return this._list.IndexOf(value); } public virtual void Insert(int index, object obj) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public virtual void Remove(object value) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public virtual void RemoveAt(int index) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } } [Serializable] private class FixedSizeArrayList : ArrayList { private ArrayList _list; public override int Count { get { return this._list.Count; } } public override bool IsReadOnly { get { return this._list.IsReadOnly; } } public override bool IsFixedSize { get { return true; } } public override bool IsSynchronized { get { return this._list.IsSynchronized; } } public override object this[int index] { get { return this._list[index]; } set { this._list[index] = value; this._version = this._list._version; } } public override object SyncRoot { get { return this._list.SyncRoot; } } public override int Capacity { get { return this._list.Capacity; } set { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } } internal FixedSizeArrayList(ArrayList l) { this._list = l; this._version = this._list._version; } public override int Add(object obj) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public override void AddRange(ICollection c) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public override int BinarySearch(int index, int count, object value, IComparer comparer) { return this._list.BinarySearch(index, count, value, comparer); } public override void Clear() { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public override object Clone() { return new ArrayList.FixedSizeArrayList(this._list) { _list = (ArrayList)this._list.Clone() }; } public override bool Contains(object obj) { return this._list.Contains(obj); } public override void CopyTo(Array array, int index) { this._list.CopyTo(array, index); } public override void CopyTo(int index, Array array, int arrayIndex, int count) { this._list.CopyTo(index, array, arrayIndex, count); } public override IEnumerator GetEnumerator() { return this._list.GetEnumerator(); } public override IEnumerator GetEnumerator(int index, int count) { return this._list.GetEnumerator(index, count); } public override int IndexOf(object value) { return this._list.IndexOf(value); } public override int IndexOf(object value, int startIndex) { return this._list.IndexOf(value, startIndex); } public override int IndexOf(object value, int startIndex, int count) { return this._list.IndexOf(value, startIndex, count); } public override void Insert(int index, object obj) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public override void InsertRange(int index, ICollection c) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public override int LastIndexOf(object value) { return this._list.LastIndexOf(value); } public override int LastIndexOf(object value, int startIndex) { return this._list.LastIndexOf(value, startIndex); } public override int LastIndexOf(object value, int startIndex, int count) { return this._list.LastIndexOf(value, startIndex, count); } public override void Remove(object value) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public override void RemoveAt(int index) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public override void RemoveRange(int index, int count) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } public override void SetRange(int index, ICollection c) { this._list.SetRange(index, c); this._version = this._list._version; } public override ArrayList GetRange(int index, int count) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this.Count - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } return new ArrayList.Range(this, index, count); } public override void Reverse(int index, int count) { this._list.Reverse(index, count); this._version = this._list._version; } public override void Sort(int index, int count, IComparer comparer) { this._list.Sort(index, count, comparer); this._version = this._list._version; } public override object[] ToArray() { return this._list.ToArray(); } public override Array ToArray(Type type) { return this._list.ToArray(type); } public override void TrimToSize() { throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); } } [Serializable] private class ReadOnlyList : IList, ICollection, IEnumerable { private IList _list; public virtual int Count { get { return this._list.Count; } } public virtual bool IsReadOnly { get { return true; } } public virtual bool IsFixedSize { get { return true; } } public virtual bool IsSynchronized { get { return this._list.IsSynchronized; } } public virtual object this[int index] { get { return this._list[index]; } set { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } } public virtual object SyncRoot { get { return this._list.SyncRoot; } } internal ReadOnlyList(IList l) { this._list = l; } public virtual int Add(object obj) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public virtual void Clear() { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public virtual bool Contains(object obj) { return this._list.Contains(obj); } public virtual void CopyTo(Array array, int index) { this._list.CopyTo(array, index); } public virtual IEnumerator GetEnumerator() { return this._list.GetEnumerator(); } public virtual int IndexOf(object value) { return this._list.IndexOf(value); } public virtual void Insert(int index, object obj) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public virtual void Remove(object value) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public virtual void RemoveAt(int index) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } } [Serializable] private class ReadOnlyArrayList : ArrayList { private ArrayList _list; public override int Count { get { return this._list.Count; } } public override bool IsReadOnly { get { return true; } } public override bool IsFixedSize { get { return true; } } public override bool IsSynchronized { get { return this._list.IsSynchronized; } } public override object this[int index] { get { return this._list[index]; } set { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } } public override object SyncRoot { get { return this._list.SyncRoot; } } public override int Capacity { get { return this._list.Capacity; } set { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } } internal ReadOnlyArrayList(ArrayList l) { this._list = l; } public override int Add(object obj) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public override void AddRange(ICollection c) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public override int BinarySearch(int index, int count, object value, IComparer comparer) { return this._list.BinarySearch(index, count, value, comparer); } public override void Clear() { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public override object Clone() { return new ArrayList.ReadOnlyArrayList(this._list) { _list = (ArrayList)this._list.Clone() }; } public override bool Contains(object obj) { return this._list.Contains(obj); } public override void CopyTo(Array array, int index) { this._list.CopyTo(array, index); } public override void CopyTo(int index, Array array, int arrayIndex, int count) { this._list.CopyTo(index, array, arrayIndex, count); } public override IEnumerator GetEnumerator() { return this._list.GetEnumerator(); } public override IEnumerator GetEnumerator(int index, int count) { return this._list.GetEnumerator(index, count); } public override int IndexOf(object value) { return this._list.IndexOf(value); } public override int IndexOf(object value, int startIndex) { return this._list.IndexOf(value, startIndex); } public override int IndexOf(object value, int startIndex, int count) { return this._list.IndexOf(value, startIndex, count); } public override void Insert(int index, object obj) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public override void InsertRange(int index, ICollection c) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public override int LastIndexOf(object value) { return this._list.LastIndexOf(value); } public override int LastIndexOf(object value, int startIndex) { return this._list.LastIndexOf(value, startIndex); } public override int LastIndexOf(object value, int startIndex, int count) { return this._list.LastIndexOf(value, startIndex, count); } public override void Remove(object value) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public override void RemoveAt(int index) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public override void RemoveRange(int index, int count) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public override void SetRange(int index, ICollection c) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public override ArrayList GetRange(int index, int count) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this.Count - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } return new ArrayList.Range(this, index, count); } public override void Reverse(int index, int count) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public override void Sort(int index, int count, IComparer comparer) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } public override object[] ToArray() { return this._list.ToArray(); } public override Array ToArray(Type type) { return this._list.ToArray(type); } public override void TrimToSize() { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); } } [Serializable] private sealed class ArrayListEnumerator : IEnumerator, ICloneable { private ArrayList list; private int index; private int endIndex; private int version; private object currentElement; private int startIndex; public object Current { get { if (this.index < this.startIndex) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted")); } if (this.index > this.endIndex) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded")); } return this.currentElement; } } internal ArrayListEnumerator(ArrayList list, int index, int count) { this.list = list; this.startIndex = index; this.index = index - 1; this.endIndex = this.index + count; this.version = list._version; this.currentElement = null; } public object Clone() { return base.MemberwiseClone(); } public bool MoveNext() { if (this.version != this.list._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } if (this.index < this.endIndex) { this.currentElement = this.list[++this.index]; return true; } this.index = this.endIndex + 1; return false; } public void Reset() { if (this.version != this.list._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } this.index = this.startIndex - 1; } } [Serializable] private class Range : ArrayList { private ArrayList _baseList; private int _baseIndex; private int _baseSize; private int _baseVersion; public override int Capacity { get { return this._baseList.Capacity; } set { if (value < this.Count) { throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_SmallCapacity")); } } } public override int Count { get { this.InternalUpdateRange(); return this._baseSize; } } public override bool IsReadOnly { get { return this._baseList.IsReadOnly; } } public override bool IsFixedSize { get { return this._baseList.IsFixedSize; } } public override bool IsSynchronized { get { return this._baseList.IsSynchronized; } } public override object SyncRoot { get { return this._baseList.SyncRoot; } } public override object this[int index] { get { this.InternalUpdateRange(); if (index < 0 || index >= this._baseSize) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } return this._baseList[this._baseIndex + index]; } set { this.InternalUpdateRange(); if (index < 0 || index >= this._baseSize) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } this._baseList[this._baseIndex + index] = value; this.InternalUpdateVersion(); } } internal Range(ArrayList list, int index, int count) : base(false) { this._baseList = list; this._baseIndex = index; this._baseSize = count; this._baseVersion = list._version; this._version = list._version; } private void InternalUpdateRange() { if (this._baseVersion != this._baseList._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnderlyingArrayListChanged")); } } private void InternalUpdateVersion() { this._baseVersion++; this._version++; } public override int Add(object value) { this.InternalUpdateRange(); this._baseList.Insert(this._baseIndex + this._baseSize, value); this.InternalUpdateVersion(); return this._baseSize++; } public override void AddRange(ICollection c) { this.InternalUpdateRange(); if (c == null) { throw new ArgumentNullException("c"); } int count = c.Count; if (count > 0) { this._baseList.InsertRange(this._baseIndex + this._baseSize, c); this.InternalUpdateVersion(); this._baseSize += count; } } public override int BinarySearch(int index, int count, object value, IComparer comparer) { this.InternalUpdateRange(); if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._baseSize - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } int num = this._baseList.BinarySearch(this._baseIndex + index, count, value, comparer); if (num >= 0) { return num - this._baseIndex; } return num + this._baseIndex; } public override void Clear() { this.InternalUpdateRange(); if (this._baseSize != 0) { this._baseList.RemoveRange(this._baseIndex, this._baseSize); this.InternalUpdateVersion(); this._baseSize = 0; } } public override object Clone() { this.InternalUpdateRange(); return new ArrayList.Range(this._baseList, this._baseIndex, this._baseSize) { _baseList = (ArrayList)this._baseList.Clone() }; } public override bool Contains(object item) { this.InternalUpdateRange(); if (item == null) { for (int i = 0; i < this._baseSize; i++) { if (this._baseList[this._baseIndex + i] == null) { return true; } } return false; } for (int j = 0; j < this._baseSize; j++) { if (this._baseList[this._baseIndex + j] != null && this._baseList[this._baseIndex + j].Equals(item)) { return true; } } return false; } public override void CopyTo(Array array, int index) { this.InternalUpdateRange(); if (array == null) { throw new ArgumentNullException("array"); } if (array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } if (index < 0) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (array.Length - index < this._baseSize) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } this._baseList.CopyTo(this._baseIndex, array, index, this._baseSize); } public override void CopyTo(int index, Array array, int arrayIndex, int count) { this.InternalUpdateRange(); if (array == null) { throw new ArgumentNullException("array"); } if (array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (array.Length - arrayIndex < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } if (this._baseSize - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } this._baseList.CopyTo(this._baseIndex + index, array, arrayIndex, count); } public override IEnumerator GetEnumerator() { return this.GetEnumerator(0, this._baseSize); } public override IEnumerator GetEnumerator(int index, int count) { this.InternalUpdateRange(); if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._baseSize - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } return this._baseList.GetEnumerator(this._baseIndex + index, count); } public override ArrayList GetRange(int index, int count) { this.InternalUpdateRange(); if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._baseSize - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } return new ArrayList.Range(this, index, count); } public override int IndexOf(object value) { this.InternalUpdateRange(); int num = this._baseList.IndexOf(value, this._baseIndex, this._baseSize); if (num >= 0) { return num - this._baseIndex; } return -1; } public override int IndexOf(object value, int startIndex) { this.InternalUpdateRange(); if (startIndex < 0) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (startIndex > this._baseSize) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); } int num = this._baseList.IndexOf(value, this._baseIndex + startIndex, this._baseSize - startIndex); if (num >= 0) { return num - this._baseIndex; } return -1; } public override int IndexOf(object value, int startIndex, int count) { this.InternalUpdateRange(); if (startIndex < 0 || startIndex > this._baseSize) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (count < 0 || startIndex > this._baseSize - count) { throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); } int num = this._baseList.IndexOf(value, this._baseIndex + startIndex, count); if (num >= 0) { return num - this._baseIndex; } return -1; } public override void Insert(int index, object value) { this.InternalUpdateRange(); if (index < 0 || index > this._baseSize) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } this._baseList.Insert(this._baseIndex + index, value); this.InternalUpdateVersion(); this._baseSize++; } public override void InsertRange(int index, ICollection c) { this.InternalUpdateRange(); if (index < 0 || index > this._baseSize) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (c == null) { throw new ArgumentNullException("c"); } int count = c.Count; if (count > 0) { this._baseList.InsertRange(this._baseIndex + index, c); this._baseSize += count; this.InternalUpdateVersion(); } } public override int LastIndexOf(object value) { this.InternalUpdateRange(); int num = this._baseList.LastIndexOf(value, this._baseIndex + this._baseSize - 1, this._baseSize); if (num >= 0) { return num - this._baseIndex; } return -1; } public override int LastIndexOf(object value, int startIndex) { return this.LastIndexOf(value, startIndex, startIndex + 1); } public override int LastIndexOf(object value, int startIndex, int count) { this.InternalUpdateRange(); if (this._baseSize == 0) { return -1; } if (startIndex >= this._baseSize) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (startIndex < 0) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } int num = this._baseList.LastIndexOf(value, this._baseIndex + startIndex, count); if (num >= 0) { return num - this._baseIndex; } return -1; } public override void RemoveAt(int index) { this.InternalUpdateRange(); if (index < 0 || index >= this._baseSize) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } this._baseList.RemoveAt(this._baseIndex + index); this.InternalUpdateVersion(); this._baseSize--; } public override void RemoveRange(int index, int count) { this.InternalUpdateRange(); if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._baseSize - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } if (count > 0) { this._baseList.RemoveRange(this._baseIndex + index, count); this.InternalUpdateVersion(); this._baseSize -= count; } } public override void Reverse(int index, int count) { this.InternalUpdateRange(); if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._baseSize - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } this._baseList.Reverse(this._baseIndex + index, count); this.InternalUpdateVersion(); } public override void SetRange(int index, ICollection c) { this.InternalUpdateRange(); if (index < 0 || index >= this._baseSize) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } this._baseList.SetRange(this._baseIndex + index, c); if (c.Count > 0) { this.InternalUpdateVersion(); } } public override void Sort(int index, int count, IComparer comparer) { this.InternalUpdateRange(); if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._baseSize - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } this._baseList.Sort(this._baseIndex + index, count, comparer); this.InternalUpdateVersion(); } public override object[] ToArray() { this.InternalUpdateRange(); object[] array = new object[this._baseSize]; Array.Copy(this._baseList._items, this._baseIndex, array, 0, this._baseSize); return array; } public override Array ToArray(Type type) { this.InternalUpdateRange(); if (type == null) { throw new ArgumentNullException("type"); } Array array = Array.CreateInstance(type, this._baseSize); this._baseList.CopyTo(this._baseIndex, array, 0, this._baseSize); return array; } public override void TrimToSize() { throw new NotSupportedException(Environment.GetResourceString("NotSupported_RangeCollection")); } } [Serializable] private sealed class ArrayListEnumeratorSimple : IEnumerator, ICloneable { private ArrayList list; private int index; private int version; private object currentElement; [NonSerialized] private bool isArrayList; private static object dummyObject = new object(); public object Current { get { object obj = this.currentElement; if (ArrayList.ArrayListEnumeratorSimple.dummyObject != obj) { return obj; } if (this.index == -1) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted")); } throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded")); } } internal ArrayListEnumeratorSimple(ArrayList list) { this.list = list; this.index = -1; this.version = list._version; this.isArrayList = (list.GetType() == typeof(ArrayList)); this.currentElement = ArrayList.ArrayListEnumeratorSimple.dummyObject; } public object Clone() { return base.MemberwiseClone(); } public bool MoveNext() { if (this.version != this.list._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } if (this.isArrayList) { if (this.index < this.list._size - 1) { this.currentElement = this.list._items[++this.index]; return true; } this.currentElement = ArrayList.ArrayListEnumeratorSimple.dummyObject; this.index = this.list._size; return false; } else { if (this.index < this.list.Count - 1) { this.currentElement = this.list[++this.index]; return true; } this.index = this.list.Count; this.currentElement = ArrayList.ArrayListEnumeratorSimple.dummyObject; return false; } } public void Reset() { if (this.version != this.list._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } this.currentElement = ArrayList.ArrayListEnumeratorSimple.dummyObject; this.index = -1; } } internal class ArrayListDebugView { private ArrayList arrayList; [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public object[] Items { get { return this.arrayList.ToArray(); } } public ArrayListDebugView(ArrayList arrayList) { if (arrayList == null) { throw new ArgumentNullException("arrayList"); } this.arrayList = arrayList; } } private const int _defaultCapacity = 4; private object[] _items; private int _size; private int _version; [NonSerialized] private object _syncRoot; private static readonly object[] emptyArray = new object[0]; /// <summary> /// Gets or sets the number of elements that the <see cref="T:System.Collections.ArrayList" /> can contain. /// </summary> /// <returns> /// The number of elements that the <see cref="T:System.Collections.ArrayList" /> can contain. /// </returns> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <see cref="P:System.Collections.ArrayList.Capacity" /> is set to a value that is less than <see cref="P:System.Collections.ArrayList.Count" />. /// </exception> /// <exception cref="T:System.OutOfMemoryException"> /// There is not enough memory available on the system. /// </exception> /// <filterpriority>1</filterpriority> public virtual int Capacity { get { return this._items.Length; } set { if (value != this._items.Length) { if (value < this._size) { throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_SmallCapacity")); } if (value > 0) { object[] array = new object[value]; if (this._size > 0) { Array.Copy(this._items, 0, array, 0, this._size); } this._items = array; return; } this._items = new object[4]; } } } /// <summary> /// Gets the number of elements actually contained in the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <returns> /// The number of elements actually contained in the <see cref="T:System.Collections.ArrayList" />. /// </returns> /// <filterpriority>1</filterpriority> public virtual int Count { get { return this._size; } } /// <summary> /// Gets a value indicating whether the <see cref="T:System.Collections.ArrayList" /> has a fixed size. /// </summary> /// <returns>true if the <see cref="T:System.Collections.ArrayList" /> has a fixed size; otherwise, false. The default is false. /// </returns> /// <filterpriority>2</filterpriority> public virtual bool IsFixedSize { get { return false; } } /// <summary> /// Gets a value indicating whether the <see cref="T:System.Collections.ArrayList" /> is read-only. /// </summary> /// <returns>true if the <see cref="T:System.Collections.ArrayList" /> is read-only; otherwise, false. The default is false. /// </returns> /// <filterpriority>2</filterpriority> public virtual bool IsReadOnly { get { return false; } } /// <summary> /// Gets a value indicating whether access to the <see cref="T:System.Collections.ArrayList" /> is synchronized (thread safe). /// </summary> /// <returns>true if access to the <see cref="T:System.Collections.ArrayList" /> is synchronized (thread safe); otherwise, false. The default is false. /// </returns> /// <filterpriority>2</filterpriority> public virtual bool IsSynchronized { get { return false; } } /// <summary> /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <returns> /// An object that can be used to synchronize access to the <see cref="T:System.Collections.ArrayList" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual object SyncRoot { get { if (this._syncRoot == null) { Interlocked.CompareExchange(ref this._syncRoot, new object(), null); } return this._syncRoot; } } /// <summary> /// Gets or sets the element at the specified index. /// </summary> /// <returns> /// The element at the specified index. /// </returns> /// <param name="index"> /// The zero-based index of the element to get or set. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="index" /> is equal to or greater than <see cref="P:System.Collections.ArrayList.Count" />. /// </exception> /// <filterpriority>1</filterpriority> public virtual object this[int index] { get { if (index < 0 || index >= this._size) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } return this._items[index]; } set { if (index < 0 || index >= this._size) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } this._items[index] = value; this._version++; } } internal ArrayList(bool trash) { } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.ArrayList" /> class that is empty and has the default initial capacity. /// </summary> public ArrayList() { this._items = ArrayList.emptyArray; } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.ArrayList" /> class that is empty and has the specified initial capacity. /// </summary> /// <param name="capacity"> /// The number of elements that the new list can initially store. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="capacity" /> is less than zero. /// </exception> public ArrayList(int capacity) { if (capacity < 0) { throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_MustBeNonNegNum", new object[] { "capacity" })); } this._items = new object[capacity]; } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.ArrayList" /> class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied. /// </summary> /// <param name="c"> /// The <see cref="T:System.Collections.ICollection" /> whose elements are copied to the new list. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="c" /> is null. /// </exception> public ArrayList(ICollection c) { if (c == null) { throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection")); } this._items = new object[c.Count]; this.AddRange(c); } /// <summary> /// Creates an <see cref="T:System.Collections.ArrayList" /> wrapper for a specific <see cref="T:System.Collections.IList" />. /// </summary> /// <returns> /// The <see cref="T:System.Collections.ArrayList" /> wrapper around the <see cref="T:System.Collections.IList" />. /// </returns> /// <param name="list"> /// The <see cref="T:System.Collections.IList" /> to wrap. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="list" /> is null. /// </exception> /// <filterpriority>2</filterpriority> public static ArrayList Adapter(IList list) { if (list == null) { throw new ArgumentNullException("list"); } return new ArrayList.IListWrapper(list); } /// <summary> /// Adds an object to the end of the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <returns> /// The <see cref="T:System.Collections.ArrayList" /> index at which the <paramref name="value" /> has been added. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to be added to the end of the <see cref="T:System.Collections.ArrayList" />. The value can be null. /// </param> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.ArrayList" /> has a fixed size. /// </exception> /// <filterpriority>1</filterpriority> public virtual int Add(object value) { if (this._size == this._items.Length) { this.EnsureCapacity(this._size + 1); } this._items[this._size] = value; this._version++; return this._size++; } /// <summary> /// Adds the elements of an <see cref="T:System.Collections.ICollection" /> to the end of the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <param name="c"> /// The <see cref="T:System.Collections.ICollection" /> whose elements should be added to the end of the <see cref="T:System.Collections.ArrayList" />. The collection itself cannot be null, but it can contain elements that are null. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="c" /> is null. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.ArrayList" /> has a fixed size. /// </exception> /// <filterpriority>1</filterpriority> public virtual void AddRange(ICollection c) { this.InsertRange(this._size, c); } /// <summary> /// Searches a range of elements in the sorted <see cref="T:System.Collections.ArrayList" /> for an element using the specified comparer and returns the zero-based index of the element. /// </summary> /// <returns> /// The zero-based index of <paramref name="value" /> in the sorted <see cref="T:System.Collections.ArrayList" />, if <paramref name="value" /> is found; otherwise, a negative number, which is the bitwise complement of the index of the next element that is larger than <paramref name="value" /> or, if there is no larger element, the bitwise complement of <see cref="P:System.Collections.ArrayList.Count" />. /// </returns> /// <param name="index"> /// The zero-based starting index of the range to search. /// </param> /// <param name="count"> /// The length of the range to search. /// </param> /// <param name="value"> /// The <see cref="T:System.Object" /> to locate. The value can be null. /// </param> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> implementation to use when comparing elements. /// /// -or- /// null to use the default comparer that is the <see cref="T:System.IComparable" /> implementation of each element. /// </param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="index" /> and <paramref name="count" /> do not denote a valid range in the <see cref="T:System.Collections.ArrayList" />. /// /// -or- /// <paramref name="comparer" /> is null and neither <paramref name="value" /> nor the elements of <see cref="T:System.Collections.ArrayList" /> implement the <see cref="T:System.IComparable" /> interface. /// </exception> /// <exception cref="T:System.InvalidOperationException"> /// <paramref name="comparer" /> is null and <paramref name="value" /> is not of the same type as the elements of the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="count" /> is less than zero. /// </exception> /// <filterpriority>1</filterpriority> public virtual int BinarySearch(int index, int count, object value, IComparer comparer) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._size - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } return Array.BinarySearch(this._items, index, count, value, comparer); } /// <summary> /// Searches the entire sorted <see cref="T:System.Collections.ArrayList" /> for an element using the default comparer and returns the zero-based index of the element. /// </summary> /// <returns> /// The zero-based index of <paramref name="value" /> in the sorted <see cref="T:System.Collections.ArrayList" />, if <paramref name="value" /> is found; otherwise, a negative number, which is the bitwise complement of the index of the next element that is larger than <paramref name="value" /> or, if there is no larger element, the bitwise complement of <see cref="P:System.Collections.ArrayList.Count" />. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to locate. The value can be null. /// </param> /// <exception cref="T:System.ArgumentException"> /// Neither <paramref name="value" /> nor the elements of <see cref="T:System.Collections.ArrayList" /> implement the <see cref="T:System.IComparable" /> interface. /// </exception> /// <exception cref="T:System.InvalidOperationException"> /// <paramref name="value" /> is not of the same type as the elements of the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <filterpriority>1</filterpriority> public virtual int BinarySearch(object value) { return this.BinarySearch(0, this.Count, value, null); } /// <summary> /// Searches the entire sorted <see cref="T:System.Collections.ArrayList" /> for an element using the specified comparer and returns the zero-based index of the element. /// </summary> /// <returns> /// The zero-based index of <paramref name="value" /> in the sorted <see cref="T:System.Collections.ArrayList" />, if <paramref name="value" /> is found; otherwise, a negative number, which is the bitwise complement of the index of the next element that is larger than <paramref name="value" /> or, if there is no larger element, the bitwise complement of <see cref="P:System.Collections.ArrayList.Count" />. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to locate. The value can be null. /// </param> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> implementation to use when comparing elements. /// /// -or- /// null to use the default comparer that is the <see cref="T:System.IComparable" /> implementation of each element. /// </param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="comparer" /> is null and neither <paramref name="value" /> nor the elements of <see cref="T:System.Collections.ArrayList" /> implement the <see cref="T:System.IComparable" /> interface. /// </exception> /// <exception cref="T:System.InvalidOperationException"> /// <paramref name="comparer" /> is null and <paramref name="value" /> is not of the same type as the elements of the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <filterpriority>1</filterpriority> public virtual int BinarySearch(object value, IComparer comparer) { return this.BinarySearch(0, this.Count, value, comparer); } /// <summary> /// Removes all elements from the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.ArrayList" /> has a fixed size. /// </exception> /// <filterpriority>1</filterpriority> public virtual void Clear() { if (this._size > 0) { Array.Clear(this._items, 0, this._size); this._size = 0; } this._version++; } /// <summary> /// Creates a shallow copy of the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <returns> /// A shallow copy of the <see cref="T:System.Collections.ArrayList" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual object Clone() { ArrayList arrayList = new ArrayList(this._size); arrayList._size = this._size; arrayList._version = this._version; Array.Copy(this._items, 0, arrayList._items, 0, this._size); return arrayList; } /// <summary> /// Determines whether an element is in the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <returns>true if <paramref name="item" /> is found in the <see cref="T:System.Collections.ArrayList" />; otherwise, false. /// </returns> /// <param name="item"> /// The <see cref="T:System.Object" /> to locate in the <see cref="T:System.Collections.ArrayList" />. The value can be null. /// </param> /// <filterpriority>1</filterpriority> public virtual bool Contains(object item) { if (item == null) { for (int i = 0; i < this._size; i++) { if (this._items[i] == null) { return true; } } return false; } for (int j = 0; j < this._size; j++) { if (this._items[j] != null && this._items[j].Equals(item)) { return true; } } return false; } /// <summary> /// Copies the entire <see cref="T:System.Collections.ArrayList" /> to a compatible one-dimensional <see cref="T:System.Array" />, starting at the beginning of the target array. /// </summary> /// <param name="array"> /// The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.ArrayList" />. The <see cref="T:System.Array" /> must have zero-based indexing. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="array" /> is null. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="array" /> is multidimensional. /// /// -or- /// /// The number of elements in the source <see cref="T:System.Collections.ArrayList" /> is greater than the number of elements that the destination <paramref name="array" /> can contain. /// </exception> /// <exception cref="T:System.InvalidCastException"> /// The type of the source <see cref="T:System.Collections.ArrayList" /> cannot be cast automatically to the type of the destination <paramref name="array" />. /// </exception> /// <filterpriority>2</filterpriority> public virtual void CopyTo(Array array) { this.CopyTo(array, 0); } /// <summary> /// Copies the entire <see cref="T:System.Collections.ArrayList" /> to a compatible one-dimensional <see cref="T:System.Array" />, starting at the specified index of the target array. /// </summary> /// <param name="array"> /// The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.ArrayList" />. The <see cref="T:System.Array" /> must have zero-based indexing. /// </param> /// <param name="arrayIndex"> /// The zero-based index in <paramref name="array" /> at which copying begins. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="array" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="arrayIndex" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="array" /> is multidimensional. /// /// -or- /// <paramref name="arrayIndex" /> is equal to or greater than the length of <paramref name="array" />. /// /// -or- /// /// The number of elements in the source <see cref="T:System.Collections.ArrayList" /> is greater than the available space from <paramref name="arrayIndex" /> to the end of the destination <paramref name="array" />. /// </exception> /// <exception cref="T:System.InvalidCastException"> /// The type of the source <see cref="T:System.Collections.ArrayList" /> cannot be cast automatically to the type of the destination <paramref name="array" />. /// </exception> /// <filterpriority>2</filterpriority> public virtual void CopyTo(Array array, int arrayIndex) { if (array != null && array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } Array.Copy(this._items, 0, array, arrayIndex, this._size); } /// <summary> /// Copies a range of elements from the <see cref="T:System.Collections.ArrayList" /> to a compatible one-dimensional <see cref="T:System.Array" />, starting at the specified index of the target array. /// </summary> /// <param name="index"> /// The zero-based index in the source <see cref="T:System.Collections.ArrayList" /> at which copying begins. /// </param> /// <param name="array"> /// The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.ArrayList" />. The <see cref="T:System.Array" /> must have zero-based indexing. /// </param> /// <param name="arrayIndex"> /// The zero-based index in <paramref name="array" /> at which copying begins. /// </param> /// <param name="count"> /// The number of elements to copy. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="array" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="arrayIndex" /> is less than zero. /// /// -or- /// <paramref name="count" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="array" /> is multidimensional. /// /// -or- /// <paramref name="index" /> is equal to or greater than the <see cref="P:System.Collections.ArrayList.Count" /> of the source <see cref="T:System.Collections.ArrayList" />. /// /// -or- /// <paramref name="arrayIndex" /> is equal to or greater than the length of <paramref name="array" />. /// /// -or- /// /// The number of elements from <paramref name="index" /> to the end of the source <see cref="T:System.Collections.ArrayList" /> is greater than the available space from <paramref name="arrayIndex" /> to the end of the destination <paramref name="array" />. /// </exception> /// <exception cref="T:System.InvalidCastException"> /// The type of the source <see cref="T:System.Collections.ArrayList" /> cannot be cast automatically to the type of the destination <paramref name="array" />. /// </exception> /// <filterpriority>2</filterpriority> public virtual void CopyTo(int index, Array array, int arrayIndex, int count) { if (this._size - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } if (array != null && array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } Array.Copy(this._items, index, array, arrayIndex, count); } private void EnsureCapacity(int min) { if (this._items.Length < min) { int num = (this._items.Length == 0) ? 4 : (this._items.Length * 2); if (num < min) { num = min; } this.Capacity = num; } } /// <summary> /// Returns an <see cref="T:System.Collections.IList" /> wrapper with a fixed size. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IList" /> wrapper with a fixed size. /// </returns> /// <param name="list"> /// The <see cref="T:System.Collections.IList" /> to wrap. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="list" /> is null. /// </exception> /// <filterpriority>2</filterpriority> public static IList FixedSize(IList list) { if (list == null) { throw new ArgumentNullException("list"); } return new ArrayList.FixedSizeList(list); } /// <summary> /// Returns an <see cref="T:System.Collections.ArrayList" /> wrapper with a fixed size. /// </summary> /// <returns> /// An <see cref="T:System.Collections.ArrayList" /> wrapper with a fixed size. /// </returns> /// <param name="list"> /// The <see cref="T:System.Collections.ArrayList" /> to wrap. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="list" /> is null. /// </exception> /// <filterpriority>2</filterpriority> public static ArrayList FixedSize(ArrayList list) { if (list == null) { throw new ArgumentNullException("list"); } return new ArrayList.FixedSizeArrayList(list); } /// <summary> /// Returns an enumerator for the entire <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IEnumerator" /> for the entire <see cref="T:System.Collections.ArrayList" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual IEnumerator GetEnumerator() { return new ArrayList.ArrayListEnumeratorSimple(this); } /// <summary> /// Returns an enumerator for a range of elements in the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IEnumerator" /> for the specified range of elements in the <see cref="T:System.Collections.ArrayList" />. /// </returns> /// <param name="index"> /// The zero-based starting index of the <see cref="T:System.Collections.ArrayList" /> section that the enumerator should refer to. /// </param> /// <param name="count"> /// The number of elements in the <see cref="T:System.Collections.ArrayList" /> section that the enumerator should refer to. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="count" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="index" /> and <paramref name="count" /> do not specify a valid range in the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <filterpriority>2</filterpriority> public virtual IEnumerator GetEnumerator(int index, int count) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._size - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } return new ArrayList.ArrayListEnumerator(this, index, count); } /// <summary> /// Searches for the specified <see cref="T:System.Object" /> and returns the zero-based index of the first occurrence within the entire <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <returns> /// The zero-based index of the first occurrence of <paramref name="value" /> within the entire <see cref="T:System.Collections.ArrayList" />, if found; otherwise, -1. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to locate in the <see cref="T:System.Collections.ArrayList" />. The value can be null. /// </param> /// <filterpriority>1</filterpriority> public virtual int IndexOf(object value) { return Array.IndexOf(this._items, value, 0, this._size); } /// <summary> /// Searches for the specified <see cref="T:System.Object" /> and returns the zero-based index of the first occurrence within the range of elements in the <see cref="T:System.Collections.ArrayList" /> that extends from the specified index to the last element. /// </summary> /// <returns> /// The zero-based index of the first occurrence of <paramref name="value" /> within the range of elements in the <see cref="T:System.Collections.ArrayList" /> that extends from <paramref name="startIndex" /> to the last element, if found; otherwise, -1. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to locate in the <see cref="T:System.Collections.ArrayList" />. The value can be null. /// </param> /// <param name="startIndex"> /// The zero-based starting index of the search. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="startIndex" /> is outside the range of valid indexes for the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <filterpriority>1</filterpriority> public virtual int IndexOf(object value, int startIndex) { if (startIndex > this._size) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); } return Array.IndexOf(this._items, value, startIndex, this._size - startIndex); } /// <summary> /// Searches for the specified <see cref="T:System.Object" /> and returns the zero-based index of the first occurrence within the range of elements in the <see cref="T:System.Collections.ArrayList" /> that starts at the specified index and contains the specified number of elements. /// </summary> /// <returns> /// The zero-based index of the first occurrence of <paramref name="value" /> within the range of elements in the <see cref="T:System.Collections.ArrayList" /> that starts at <paramref name="startIndex" /> and contains <paramref name="count" /> number of elements, if found; otherwise, -1. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to locate in the <see cref="T:System.Collections.ArrayList" />. The value can be null. /// </param> /// <param name="startIndex"> /// The zero-based starting index of the search. /// </param> /// <param name="count"> /// The number of elements in the section to search. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="startIndex" /> is outside the range of valid indexes for the <see cref="T:System.Collections.ArrayList" />. /// /// -or- /// <paramref name="count" /> is less than zero. /// /// -or- /// <paramref name="startIndex" /> and <paramref name="count" /> do not specify a valid section in the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <filterpriority>1</filterpriority> public virtual int IndexOf(object value, int startIndex, int count) { if (startIndex > this._size) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (count < 0 || startIndex > this._size - count) { throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); } return Array.IndexOf(this._items, value, startIndex, count); } /// <summary> /// Inserts an element into the <see cref="T:System.Collections.ArrayList" /> at the specified index. /// </summary> /// <param name="index"> /// The zero-based index at which <paramref name="value" /> should be inserted. /// </param> /// <param name="value"> /// The <see cref="T:System.Object" /> to insert. The value can be null. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="index" /> is greater than <see cref="P:System.Collections.ArrayList.Count" />. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.ArrayList" /> has a fixed size. /// </exception> /// <filterpriority>1</filterpriority> public virtual void Insert(int index, object value) { if (index < 0 || index > this._size) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_ArrayListInsert")); } if (this._size == this._items.Length) { this.EnsureCapacity(this._size + 1); } if (index < this._size) { Array.Copy(this._items, index, this._items, index + 1, this._size - index); } this._items[index] = value; this._size++; this._version++; } /// <summary> /// Inserts the elements of a collection into the <see cref="T:System.Collections.ArrayList" /> at the specified index. /// </summary> /// <param name="index"> /// The zero-based index at which the new elements should be inserted. /// </param> /// <param name="c"> /// The <see cref="T:System.Collections.ICollection" /> whose elements should be inserted into the <see cref="T:System.Collections.ArrayList" />. The collection itself cannot be null, but it can contain elements that are null. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="c" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="index" /> is greater than <see cref="P:System.Collections.ArrayList.Count" />. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.ArrayList" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> public virtual void InsertRange(int index, ICollection c) { if (c == null) { throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection")); } if (index < 0 || index > this._size) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } int count = c.Count; if (count > 0) { this.EnsureCapacity(this._size + count); if (index < this._size) { Array.Copy(this._items, index, this._items, index + count, this._size - index); } object[] array = new object[count]; c.CopyTo(array, 0); array.CopyTo(this._items, index); this._size += count; this._version++; } } /// <summary> /// Searches for the specified <see cref="T:System.Object" /> and returns the zero-based index of the last occurrence within the entire <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <returns> /// The zero-based index of the last occurrence of <paramref name="value" /> within the entire the <see cref="T:System.Collections.ArrayList" />, if found; otherwise, -1. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to locate in the <see cref="T:System.Collections.ArrayList" />. The value can be null. /// </param> /// <filterpriority>2</filterpriority> public virtual int LastIndexOf(object value) { return this.LastIndexOf(value, this._size - 1, this._size); } /// <summary> /// Searches for the specified <see cref="T:System.Object" /> and returns the zero-based index of the last occurrence within the range of elements in the <see cref="T:System.Collections.ArrayList" /> that extends from the first element to the specified index. /// </summary> /// <returns> /// The zero-based index of the last occurrence of <paramref name="value" /> within the range of elements in the <see cref="T:System.Collections.ArrayList" /> that extends from the first element to <paramref name="startIndex" />, if found; otherwise, -1. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to locate in the <see cref="T:System.Collections.ArrayList" />. The value can be null. /// </param> /// <param name="startIndex"> /// The zero-based starting index of the backward search. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="startIndex" /> is outside the range of valid indexes for the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <filterpriority>2</filterpriority> public virtual int LastIndexOf(object value, int startIndex) { if (startIndex >= this._size) { throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); } return this.LastIndexOf(value, startIndex, startIndex + 1); } /// <summary> /// Searches for the specified <see cref="T:System.Object" /> and returns the zero-based index of the last occurrence within the range of elements in the <see cref="T:System.Collections.ArrayList" /> that contains the specified number of elements and ends at the specified index. /// </summary> /// <returns> /// The zero-based index of the last occurrence of <paramref name="value" /> within the range of elements in the <see cref="T:System.Collections.ArrayList" /> that contains <paramref name="count" /> number of elements and ends at <paramref name="startIndex" />, if found; otherwise, -1. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to locate in the <see cref="T:System.Collections.ArrayList" />. The value can be null. /// </param> /// <param name="startIndex"> /// The zero-based starting index of the backward search. /// </param> /// <param name="count"> /// The number of elements in the section to search. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="startIndex" /> is outside the range of valid indexes for the <see cref="T:System.Collections.ArrayList" />. /// /// -or- /// <paramref name="count" /> is less than zero. /// /// -or- /// <paramref name="startIndex" /> and <paramref name="count" /> do not specify a valid section in the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <filterpriority>2</filterpriority> public virtual int LastIndexOf(object value, int startIndex, int count) { if (this._size == 0) { return -1; } if (startIndex < 0 || count < 0) { throw new ArgumentOutOfRangeException((startIndex < 0) ? "startIndex" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (startIndex >= this._size || count > startIndex + 1) { throw new ArgumentOutOfRangeException((startIndex >= this._size) ? "startIndex" : "count", Environment.GetResourceString("ArgumentOutOfRange_BiggerThanCollection")); } return Array.LastIndexOf(this._items, value, startIndex, count); } /// <summary> /// Returns a read-only <see cref="T:System.Collections.IList" /> wrapper. /// </summary> /// <returns> /// A read-only <see cref="T:System.Collections.IList" /> wrapper around <paramref name="list" />. /// </returns> /// <param name="list"> /// The <see cref="T:System.Collections.IList" /> to wrap. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="list" /> is null. /// </exception> /// <filterpriority>2</filterpriority> public static IList ReadOnly(IList list) { if (list == null) { throw new ArgumentNullException("list"); } return new ArrayList.ReadOnlyList(list); } /// <summary> /// Returns a read-only <see cref="T:System.Collections.ArrayList" /> wrapper. /// </summary> /// <returns> /// A read-only <see cref="T:System.Collections.ArrayList" /> wrapper around <paramref name="list" />. /// </returns> /// <param name="list"> /// The <see cref="T:System.Collections.ArrayList" /> to wrap. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="list" /> is null. /// </exception> /// <filterpriority>2</filterpriority> public static ArrayList ReadOnly(ArrayList list) { if (list == null) { throw new ArgumentNullException("list"); } return new ArrayList.ReadOnlyArrayList(list); } /// <summary> /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <param name="obj"> /// The <see cref="T:System.Object" /> to remove from the <see cref="T:System.Collections.ArrayList" />. The value can be null. /// </param> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.ArrayList" /> has a fixed size. /// </exception> /// <filterpriority>1</filterpriority> public virtual void Remove(object obj) { int num = this.IndexOf(obj); if (num >= 0) { this.RemoveAt(num); } } /// <summary> /// Removes the element at the specified index of the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <param name="index"> /// The zero-based index of the element to remove. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="index" /> is equal to or greater than <see cref="P:System.Collections.ArrayList.Count" />. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.ArrayList" /> has a fixed size. /// </exception> /// <filterpriority>1</filterpriority> public virtual void RemoveAt(int index) { if (index < 0 || index >= this._size) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } this._size--; if (index < this._size) { Array.Copy(this._items, index + 1, this._items, index, this._size - index); } this._items[this._size] = null; this._version++; } /// <summary> /// Removes a range of elements from the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <param name="index"> /// The zero-based starting index of the range of elements to remove. /// </param> /// <param name="count"> /// The number of elements to remove. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="count" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="index" /> and <paramref name="count" /> do not denote a valid range of elements in the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.ArrayList" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> public virtual void RemoveRange(int index, int count) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._size - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } if (count > 0) { int i = this._size; this._size -= count; if (index < this._size) { Array.Copy(this._items, index + count, this._items, index, this._size - index); } while (i > this._size) { this._items[--i] = null; } this._version++; } } /// <summary> /// Returns an <see cref="T:System.Collections.ArrayList" /> whose elements are copies of the specified value. /// </summary> /// <returns> /// An <see cref="T:System.Collections.ArrayList" /> with <paramref name="count" /> number of elements, all of which are copies of <paramref name="value" />. /// </returns> /// <param name="value"> /// The <see cref="T:System.Object" /> to copy multiple times in the new <see cref="T:System.Collections.ArrayList" />. The value can be null. /// </param> /// <param name="count"> /// The number of times <paramref name="value" /> should be copied. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="count" /> is less than zero. /// </exception> /// <filterpriority>2</filterpriority> public static ArrayList Repeat(object value, int count) { if (count < 0) { throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } ArrayList arrayList = new ArrayList((count > 4) ? count : 4); for (int i = 0; i < count; i++) { arrayList.Add(value); } return arrayList; } /// <summary> /// Reverses the order of the elements in the entire <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// </exception> /// <filterpriority>2</filterpriority> public virtual void Reverse() { this.Reverse(0, this.Count); } /// <summary> /// Reverses the order of the elements in the specified range. /// </summary> /// <param name="index"> /// The zero-based starting index of the range to reverse. /// </param> /// <param name="count"> /// The number of elements in the range to reverse. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="count" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="index" /> and <paramref name="count" /> do not denote a valid range of elements in the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// </exception> /// <filterpriority>2</filterpriority> public virtual void Reverse(int index, int count) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._size - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } Array.Reverse(this._items, index, count); this._version++; } /// <summary> /// Copies the elements of a collection over a range of elements in the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <param name="index"> /// The zero-based <see cref="T:System.Collections.ArrayList" /> index at which to start copying the elements of <paramref name="c" />. /// </param> /// <param name="c"> /// The <see cref="T:System.Collections.ICollection" /> whose elements to copy to the <see cref="T:System.Collections.ArrayList" />. The collection itself cannot be null, but it can contain elements that are null. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="index" /> plus the number of elements in <paramref name="c" /> is greater than <see cref="P:System.Collections.ArrayList.Count" />. /// </exception> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="c" /> is null. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// </exception> /// <filterpriority>2</filterpriority> public virtual void SetRange(int index, ICollection c) { if (c == null) { throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection")); } int count = c.Count; if (index < 0 || index > this._size - count) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (count > 0) { c.CopyTo(this._items, index); this._version++; } } /// <summary> /// Returns an <see cref="T:System.Collections.ArrayList" /> which represents a subset of the elements in the source <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <returns> /// An <see cref="T:System.Collections.ArrayList" /> which represents a subset of the elements in the source <see cref="T:System.Collections.ArrayList" />. /// </returns> /// <param name="index"> /// The zero-based <see cref="T:System.Collections.ArrayList" /> index at which the range starts. /// </param> /// <param name="count"> /// The number of elements in the range. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="count" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="index" /> and <paramref name="count" /> do not denote a valid range of elements in the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <filterpriority>2</filterpriority> public virtual ArrayList GetRange(int index, int count) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._size - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } return new ArrayList.Range(this, index, count); } /// <summary> /// Sorts the elements in the entire <see cref="T:System.Collections.ArrayList" /> using the <see cref="T:System.IComparable" /> implementation of each element. /// </summary> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// </exception> /// <filterpriority>1</filterpriority> public virtual void Sort() { this.Sort(0, this.Count, Comparer.Default); } /// <summary> /// Sorts the elements in the entire <see cref="T:System.Collections.ArrayList" /> using the specified comparer. /// </summary> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> implementation to use when comparing elements. /// /// -or- /// null to use the <see cref="T:System.IComparable" /> implementation of each element. /// </param> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// </exception> /// <filterpriority>1</filterpriority> public virtual void Sort(IComparer comparer) { this.Sort(0, this.Count, comparer); } /// <summary> /// Sorts the elements in a range of elements in <see cref="T:System.Collections.ArrayList" /> using the specified comparer. /// </summary> /// <param name="index"> /// The zero-based starting index of the range to sort. /// </param> /// <param name="count"> /// The length of the range to sort. /// </param> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> implementation to use when comparing elements. /// /// -or- /// null to use the <see cref="T:System.IComparable" /> implementation of each element. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="count" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="index" /> and <paramref name="count" /> do not specify a valid range in the <see cref="T:System.Collections.ArrayList" />. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// </exception> /// <filterpriority>1</filterpriority> public virtual void Sort(int index, int count, IComparer comparer) { if (index < 0 || count < 0) { throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (this._size - index < count) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } Array.Sort(this._items, index, count, comparer); this._version++; } /// <summary> /// Returns an <see cref="T:System.Collections.IList" /> wrapper that is synchronized (thread safe). /// </summary> /// <returns> /// An <see cref="T:System.Collections.IList" /> wrapper that is synchronized (thread safe). /// </returns> /// <param name="list"> /// The <see cref="T:System.Collections.IList" /> to synchronize. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="list" /> is null. /// </exception> /// <filterpriority>2</filterpriority> [HostProtection(SecurityAction.LinkDemand, Synchronization = true)] public static IList Synchronized(IList list) { if (list == null) { throw new ArgumentNullException("list"); } return new ArrayList.SyncIList(list); } /// <summary> /// Returns an <see cref="T:System.Collections.ArrayList" /> wrapper that is synchronized (thread safe). /// </summary> /// <returns> /// An <see cref="T:System.Collections.ArrayList" /> wrapper that is synchronized (thread safe). /// </returns> /// <param name="list"> /// The <see cref="T:System.Collections.ArrayList" /> to synchronize. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="list" /> is null. /// </exception> /// <filterpriority>2</filterpriority> [HostProtection(SecurityAction.LinkDemand, Synchronization = true)] public static ArrayList Synchronized(ArrayList list) { if (list == null) { throw new ArgumentNullException("list"); } return new ArrayList.SyncArrayList(list); } /// <summary> /// Copies the elements of the <see cref="T:System.Collections.ArrayList" /> to a new <see cref="T:System.Object" /> array. /// </summary> /// <returns> /// An <see cref="T:System.Object" /> array containing copies of the elements of the <see cref="T:System.Collections.ArrayList" />. /// </returns> /// <filterpriority>1</filterpriority> public virtual object[] ToArray() { object[] array = new object[this._size]; Array.Copy(this._items, 0, array, 0, this._size); return array; } /// <summary> /// Copies the elements of the <see cref="T:System.Collections.ArrayList" /> to a new array of the specified element type. /// </summary> /// <returns> /// An array of the specified element type containing copies of the elements of the <see cref="T:System.Collections.ArrayList" />. /// </returns> /// <param name="type"> /// The element <see cref="T:System.Type" /> of the destination array to create and copy elements to. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="type" /> is null. /// </exception> /// <exception cref="T:System.InvalidCastException"> /// The type of the source <see cref="T:System.Collections.ArrayList" /> cannot be cast automatically to the specified type. /// </exception> /// <filterpriority>1</filterpriority> public virtual Array ToArray(Type type) { if (type == null) { throw new ArgumentNullException("type"); } Array array = Array.CreateInstance(type, this._size); Array.Copy(this._items, 0, array, 0, this._size); return array; } /// <summary> /// Sets the capacity to the actual number of elements in the <see cref="T:System.Collections.ArrayList" />. /// </summary> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.ArrayList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.ArrayList" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> public virtual void TrimToSize() { this.Capacity = this._size; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Runtime.InteropServices; using System.Threading; namespace System.Collections { /// <summary> /// Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0). /// </summary> /// <filterpriority>2</filterpriority> [ComVisible(true)] [Serializable] public sealed class BitArray : ICollection, IEnumerable, ICloneable { [Serializable] private class BitArrayEnumeratorSimple : IEnumerator, ICloneable { private BitArray bitarray; private int index; private int version; private bool currentElement; public virtual object Current { get { if (this.index == -1) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted")); } if (this.index >= this.bitarray.Count) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded")); } return this.currentElement; } } internal BitArrayEnumeratorSimple(BitArray bitarray) { this.bitarray = bitarray; this.index = -1; this.version = bitarray._version; } public object Clone() { return base.MemberwiseClone(); } public virtual bool MoveNext() { if (this.version != this.bitarray._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } if (this.index < this.bitarray.Count - 1) { this.index++; this.currentElement = this.bitarray.Get(this.index); return true; } this.index = this.bitarray.Count; return false; } public void Reset() { if (this.version != this.bitarray._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } this.index = -1; } } private const int _ShrinkThreshold = 256; private int[] m_array; private int m_length; private int _version; [NonSerialized] private object _syncRoot; /// <summary> /// Gets or sets the value of the bit at a specific position in the <see cref="T:System.Collections.BitArray" />. /// </summary> /// <returns> /// The value of the bit at position <paramref name="index" />. /// </returns> /// <param name="index"> /// The zero-based index of the value to get or set. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="index" /> is equal to or greater than <see cref="P:System.Collections.BitArray.Count" />. /// </exception> /// <filterpriority>2</filterpriority> public bool this[int index] { get { return this.Get(index); } set { this.Set(index, value); } } /// <summary> /// Gets or sets the number of elements in the <see cref="T:System.Collections.BitArray" />. /// </summary> /// <returns> /// The number of elements in the <see cref="T:System.Collections.BitArray" />. /// </returns> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// The property is set to a value that is less than zero. /// </exception> /// <filterpriority>2</filterpriority> public int Length { get { return this.m_length; } set { if (value < 0) { throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } int num = (value + 31) / 32; if (num > this.m_array.Length || num + 256 < this.m_array.Length) { int[] array = new int[num]; Array.Copy(this.m_array, array, (num > this.m_array.Length) ? this.m_array.Length : num); this.m_array = array; } if (value > this.m_length) { int num2 = (this.m_length + 31) / 32 - 1; int num3 = this.m_length % 32; if (num3 > 0) { this.m_array[num2] &= (1 << num3) - 1; } Array.Clear(this.m_array, num2 + 1, num - num2 - 1); } this.m_length = value; this._version++; } } /// <summary> /// Gets the number of elements contained in the <see cref="T:System.Collections.BitArray" />. /// </summary> /// <returns> /// The number of elements contained in the <see cref="T:System.Collections.BitArray" />. /// </returns> /// <filterpriority>2</filterpriority> public int Count { get { return this.m_length; } } /// <summary> /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.BitArray" />. /// </summary> /// <returns> /// An object that can be used to synchronize access to the <see cref="T:System.Collections.BitArray" />. /// </returns> /// <filterpriority>2</filterpriority> public object SyncRoot { get { if (this._syncRoot == null) { Interlocked.CompareExchange(ref this._syncRoot, new object(), null); } return this._syncRoot; } } /// <summary> /// Gets a value indicating whether the <see cref="T:System.Collections.BitArray" /> is read-only. /// </summary> /// <returns> /// This property is always false. /// </returns> /// <filterpriority>2</filterpriority> public bool IsReadOnly { get { return false; } } /// <summary> /// Gets a value indicating whether access to the <see cref="T:System.Collections.BitArray" /> is synchronized (thread safe). /// </summary> /// <returns> /// This property is always false. /// </returns> /// <filterpriority>2</filterpriority> public bool IsSynchronized { get { return false; } } private BitArray() { } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.BitArray" /> class that can hold the specified number of bit values, which are initially set to false. /// </summary> /// <param name="length"> /// The number of bit values in the new <see cref="T:System.Collections.BitArray" />. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="length" /> is less than zero. /// </exception> public BitArray(int length) : this(length, false) { } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.BitArray" /> class that can hold the specified number of bit values, which are initially set to the specified value. /// </summary> /// <param name="length"> /// The number of bit values in the new <see cref="T:System.Collections.BitArray" />. /// </param> /// <param name="defaultValue"> /// The Boolean value to assign to each bit. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="length" /> is less than zero. /// </exception> public BitArray(int length, bool defaultValue) { if (length < 0) { throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } this.m_array = new int[(length + 31) / 32]; this.m_length = length; int num = defaultValue ? -1 : 0; for (int i = 0; i < this.m_array.Length; i++) { this.m_array[i] = num; } this._version = 0; } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.BitArray" /> class that contains bit values copied from the specified array of bytes. /// </summary> /// <param name="bytes"> /// An array of bytes containing the values to copy, where each byte represents eight consecutive bits. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="bytes" /> is null. /// </exception> /// <exception cref="T:System.ArgumentException"> /// The length of <paramref name="bytes" /> is greater than <see cref="F:System.Int32.MaxValue" />. /// </exception> public BitArray(byte[] bytes) { if (bytes == null) { throw new ArgumentNullException("bytes"); } this.m_array = new int[(bytes.Length + 3) / 4]; this.m_length = bytes.Length * 8; int num = 0; int num2 = 0; while (bytes.Length - num2 >= 4) { this.m_array[num++] = ((int)(bytes[num2] & 255) | (int)(bytes[num2 + 1] & 255) << 8 | (int)(bytes[num2 + 2] & 255) << 16 | (int)(bytes[num2 + 3] & 255) << 24); num2 += 4; } switch (bytes.Length - num2) { case 1: goto IL_DB; case 2: break; case 3: this.m_array[num] = (int)(bytes[num2 + 2] & 255) << 16; break; default: goto IL_FC; } this.m_array[num] |= (int)(bytes[num2 + 1] & 255) << 8; IL_DB: this.m_array[num] |= (int)(bytes[num2] & 255); IL_FC: this._version = 0; } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.BitArray" /> class that contains bit values copied from the specified array of Booleans. /// </summary> /// <param name="values"> /// An array of Booleans to copy. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="values" /> is null. /// </exception> public BitArray(bool[] values) { if (values == null) { throw new ArgumentNullException("values"); } this.m_array = new int[(values.Length + 31) / 32]; this.m_length = values.Length; for (int i = 0; i < values.Length; i++) { if (values[i]) { this.m_array[i / 32] |= 1 << i % 32; } } this._version = 0; } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.BitArray" /> class that contains bit values copied from the specified array of 32-bit integers. /// </summary> /// <param name="values"> /// An array of integers containing the values to copy, where each integer represents 32 consecutive bits. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="values" /> is null. /// </exception> /// <exception cref="T:System.ArgumentException"> /// The length of <paramref name="values" /> is greater than <see cref="F:System.Int32.MaxValue" /></exception> public BitArray(int[] values) { if (values == null) { throw new ArgumentNullException("values"); } this.m_array = new int[values.Length]; this.m_length = values.Length * 32; Array.Copy(values, this.m_array, values.Length); this._version = 0; } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.BitArray" /> class that contains bit values copied from the specified <see cref="T:System.Collections.BitArray" />. /// </summary> /// <param name="bits"> /// The <see cref="T:System.Collections.BitArray" /> to copy. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="bits" /> is null. /// </exception> public BitArray(BitArray bits) { if (bits == null) { throw new ArgumentNullException("bits"); } this.m_array = new int[(bits.m_length + 31) / 32]; this.m_length = bits.m_length; Array.Copy(bits.m_array, this.m_array, (bits.m_length + 31) / 32); this._version = bits._version; } /// <summary> /// Gets the value of the bit at a specific position in the <see cref="T:System.Collections.BitArray" />. /// </summary> /// <returns> /// The value of the bit at position <paramref name="index" />. /// </returns> /// <param name="index"> /// The zero-based index of the value to get. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="index" /> is greater than or equal to the number of elements in the <see cref="T:System.Collections.BitArray" />. /// </exception> /// <filterpriority>2</filterpriority> public bool Get(int index) { if (index < 0 || index >= this.m_length) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } return (this.m_array[index / 32] & 1 << index % 32) != 0; } /// <summary> /// Sets the bit at a specific position in the <see cref="T:System.Collections.BitArray" /> to the specified value. /// </summary> /// <param name="index"> /// The zero-based index of the bit to set. /// </param> /// <param name="value"> /// The Boolean value to assign to the bit. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// /// -or- /// <paramref name="index" /> is greater than or equal to the number of elements in the <see cref="T:System.Collections.BitArray" />. /// </exception> /// <filterpriority>2</filterpriority> public void Set(int index, bool value) { if (index < 0 || index >= this.m_length) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } if (value) { this.m_array[index / 32] |= 1 << index % 32; } else { this.m_array[index / 32] &= ~(1 << index % 32); } this._version++; } /// <summary> /// Sets all bits in the <see cref="T:System.Collections.BitArray" /> to the specified value. /// </summary> /// <param name="value"> /// The Boolean value to assign to all bits. /// </param> /// <filterpriority>2</filterpriority> public void SetAll(bool value) { int num = value ? -1 : 0; int num2 = (this.m_length + 31) / 32; for (int i = 0; i < num2; i++) { this.m_array[i] = num; } this._version++; } /// <summary> /// Performs the bitwise AND operation on the elements in the current <see cref="T:System.Collections.BitArray" /> against the corresponding elements in the specified <see cref="T:System.Collections.BitArray" />. /// </summary> /// <returns> /// A <see cref="T:System.Collections.BitArray" /> containing the result of the bitwise AND operation on the elements in the current <see cref="T:System.Collections.BitArray" /> against the corresponding elements in the specified <see cref="T:System.Collections.BitArray" />. /// </returns> /// <param name="value"> /// The <see cref="T:System.Collections.BitArray" /> with which to perform the bitwise AND operation. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="value" /> is null. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="value" /> and the current <see cref="T:System.Collections.BitArray" /> do not have the same number of elements. /// </exception> /// <filterpriority>2</filterpriority> public BitArray And(BitArray value) { if (value == null) { throw new ArgumentNullException("value"); } if (this.m_length != value.m_length) { throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer")); } int num = (this.m_length + 31) / 32; for (int i = 0; i < num; i++) { this.m_array[i] &= value.m_array[i]; } this._version++; return this; } /// <summary> /// Performs the bitwise OR operation on the elements in the current <see cref="T:System.Collections.BitArray" /> against the corresponding elements in the specified <see cref="T:System.Collections.BitArray" />. /// </summary> /// <returns> /// A <see cref="T:System.Collections.BitArray" /> containing the result of the bitwise OR operation on the elements in the current <see cref="T:System.Collections.BitArray" /> against the corresponding elements in the specified <see cref="T:System.Collections.BitArray" />. /// </returns> /// <param name="value"> /// The <see cref="T:System.Collections.BitArray" /> with which to perform the bitwise OR operation. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="value" /> is null. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="value" /> and the current <see cref="T:System.Collections.BitArray" /> do not have the same number of elements. /// </exception> /// <filterpriority>2</filterpriority> public BitArray Or(BitArray value) { if (value == null) { throw new ArgumentNullException("value"); } if (this.m_length != value.m_length) { throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer")); } int num = (this.m_length + 31) / 32; for (int i = 0; i < num; i++) { this.m_array[i] |= value.m_array[i]; } this._version++; return this; } /// <summary> /// Performs the bitwise exclusive OR operation on the elements in the current <see cref="T:System.Collections.BitArray" /> against the corresponding elements in the specified <see cref="T:System.Collections.BitArray" />. /// </summary> /// <returns> /// A <see cref="T:System.Collections.BitArray" /> containing the result of the bitwise exclusive OR operation on the elements in the current <see cref="T:System.Collections.BitArray" /> against the corresponding elements in the specified <see cref="T:System.Collections.BitArray" />. /// </returns> /// <param name="value"> /// The <see cref="T:System.Collections.BitArray" /> with which to perform the bitwise exclusive OR operation. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="value" /> is null. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="value" /> and the current <see cref="T:System.Collections.BitArray" /> do not have the same number of elements. /// </exception> /// <filterpriority>2</filterpriority> public BitArray Xor(BitArray value) { if (value == null) { throw new ArgumentNullException("value"); } if (this.m_length != value.m_length) { throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer")); } int num = (this.m_length + 31) / 32; for (int i = 0; i < num; i++) { this.m_array[i] ^= value.m_array[i]; } this._version++; return this; } /// <summary> /// Inverts all the bit values in the current <see cref="T:System.Collections.BitArray" />, so that elements set to true are changed to false, and elements set to false are changed to true. /// </summary> /// <returns> /// The current instance with inverted bit values. /// </returns> /// <filterpriority>2</filterpriority> public BitArray Not() { int num = (this.m_length + 31) / 32; for (int i = 0; i < num; i++) { this.m_array[i] = ~this.m_array[i]; } this._version++; return this; } /// <summary> /// Copies the entire <see cref="T:System.Collections.BitArray" /> to a compatible one-dimensional <see cref="T:System.Array" />, starting at the specified index of the target array. /// </summary> /// <param name="array"> /// The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.BitArray" />. The <see cref="T:System.Array" /> must have zero-based indexing. /// </param> /// <param name="index"> /// The zero-based index in <paramref name="array" /> at which copying begins. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="array" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="array" /> is multidimensional. /// /// -or- /// <paramref name="index" /> is equal to or greater than the length of <paramref name="array" />. /// /// -or- /// /// The number of elements in the source <see cref="T:System.Collections.BitArray" /> is greater than the available space from <paramref name="index" /> to the end of the destination <paramref name="array" />. /// </exception> /// <exception cref="T:System.InvalidCastException"> /// The type of the source <see cref="T:System.Collections.BitArray" /> cannot be cast automatically to the type of the destination <paramref name="array" />. /// </exception> /// <filterpriority>2</filterpriority> public void CopyTo(Array array, int index) { if (array == null) { throw new ArgumentNullException("array"); } if (index < 0) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } if (array is int[]) { Array.Copy(this.m_array, 0, array, index, (this.m_length + 31) / 32); return; } if (array is byte[]) { if (array.Length - index < (this.m_length + 7) / 8) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } byte[] array2 = (byte[])array; for (int i = 0; i < (this.m_length + 7) / 8; i++) { array2[index + i] = (byte)(this.m_array[i / 4] >> i % 4 * 8 & 255); } return; } else { if (!(array is bool[])) { throw new ArgumentException(Environment.GetResourceString("Arg_BitArrayTypeUnsupported")); } if (array.Length - index < this.m_length) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } bool[] array3 = (bool[])array; for (int j = 0; j < this.m_length; j++) { array3[index + j] = ((this.m_array[j / 32] >> j % 32 & 1) != 0); } return; } } /// <summary> /// Creates a shallow copy of the <see cref="T:System.Collections.BitArray" />. /// </summary> /// <returns> /// A shallow copy of the <see cref="T:System.Collections.BitArray" />. /// </returns> /// <filterpriority>2</filterpriority> public object Clone() { return new BitArray(this.m_array) { _version = this._version, m_length = this.m_length }; } /// <summary> /// Returns an enumerator that iterates through the <see cref="T:System.Collections.BitArray" />. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IEnumerator" /> for the entire <see cref="T:System.Collections.BitArray" />. /// </returns> /// <filterpriority>2</filterpriority> public IEnumerator GetEnumerator() { return new BitArray.BitArrayEnumeratorSimple(this); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Threading; namespace System.Collections { /// <summary> /// Represents a first-in, first-out collection of objects. /// </summary> /// <filterpriority>1</filterpriority> [DebuggerDisplay("Count = {Count}"), DebuggerTypeProxy(typeof(Queue.QueueDebugView)), ComVisible(true)] [Serializable] public class Queue : ICollection, IEnumerable, ICloneable { [Serializable] private class SynchronizedQueue : Queue { private Queue _q; private object root; public override bool IsSynchronized { get { return true; } } public override object SyncRoot { get { return this.root; } } public override int Count { get { object obj; Monitor.Enter(obj = this.root); int count; try { count = this._q.Count; } finally { Monitor.Exit(obj); } return count; } } internal SynchronizedQueue(Queue q) { this._q = q; this.root = this._q.SyncRoot; } public override void Clear() { object obj; Monitor.Enter(obj = this.root); try { this._q.Clear(); } finally { Monitor.Exit(obj); } } public override object Clone() { object obj; Monitor.Enter(obj = this.root); object result; try { result = new Queue.SynchronizedQueue((Queue)this._q.Clone()); } finally { Monitor.Exit(obj); } return result; } public override bool Contains(object obj) { object obj2; Monitor.Enter(obj2 = this.root); bool result; try { result = this._q.Contains(obj); } finally { Monitor.Exit(obj2); } return result; } public override void CopyTo(Array array, int arrayIndex) { object obj; Monitor.Enter(obj = this.root); try { this._q.CopyTo(array, arrayIndex); } finally { Monitor.Exit(obj); } } public override void Enqueue(object value) { object obj; Monitor.Enter(obj = this.root); try { this._q.Enqueue(value); } finally { Monitor.Exit(obj); } } public override object Dequeue() { object obj; Monitor.Enter(obj = this.root); object result; try { result = this._q.Dequeue(); } finally { Monitor.Exit(obj); } return result; } public override IEnumerator GetEnumerator() { object obj; Monitor.Enter(obj = this.root); IEnumerator enumerator; try { enumerator = this._q.GetEnumerator(); } finally { Monitor.Exit(obj); } return enumerator; } public override object Peek() { object obj; Monitor.Enter(obj = this.root); object result; try { result = this._q.Peek(); } finally { Monitor.Exit(obj); } return result; } public override object[] ToArray() { object obj; Monitor.Enter(obj = this.root); object[] result; try { result = this._q.ToArray(); } finally { Monitor.Exit(obj); } return result; } public override void TrimToSize() { object obj; Monitor.Enter(obj = this.root); try { this._q.TrimToSize(); } finally { Monitor.Exit(obj); } } } [Serializable] private class QueueEnumerator : IEnumerator, ICloneable { private Queue _q; private int _index; private int _version; private object currentElement; public virtual object Current { get { if (this.currentElement != this._q._array) { return this.currentElement; } if (this._index == 0) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted")); } throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded")); } } internal QueueEnumerator(Queue q) { this._q = q; this._version = this._q._version; this._index = 0; this.currentElement = this._q._array; if (this._q._size == 0) { this._index = -1; } } public object Clone() { return base.MemberwiseClone(); } public virtual bool MoveNext() { if (this._version != this._q._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } if (this._index < 0) { this.currentElement = this._q._array; return false; } this.currentElement = this._q.GetElement(this._index); this._index++; if (this._index == this._q._size) { this._index = -1; } return true; } public virtual void Reset() { if (this._version != this._q._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } if (this._q._size == 0) { this._index = -1; } else { this._index = 0; } this.currentElement = this._q._array; } } internal class QueueDebugView { private Queue queue; [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public object[] Items { get { return this.queue.ToArray(); } } public QueueDebugView(Queue queue) { if (queue == null) { throw new ArgumentNullException("queue"); } this.queue = queue; } } private const int _MinimumGrow = 4; private const int _ShrinkThreshold = 32; private object[] _array; private int _head; private int _tail; private int _size; private int _growFactor; private int _version; [NonSerialized] private object _syncRoot; /// <summary> /// Gets the number of elements contained in the <see cref="T:System.Collections.Queue" />. /// </summary> /// <returns> /// The number of elements contained in the <see cref="T:System.Collections.Queue" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual int Count { get { return this._size; } } /// <summary> /// Gets a value indicating whether access to the <see cref="T:System.Collections.Queue" /> is synchronized (thread safe). /// </summary> /// <returns>true if access to the <see cref="T:System.Collections.Queue" /> is synchronized (thread safe); otherwise, false. The default is false. /// </returns> /// <filterpriority>2</filterpriority> public virtual bool IsSynchronized { get { return false; } } /// <summary> /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.Queue" />. /// </summary> /// <returns> /// An object that can be used to synchronize access to the <see cref="T:System.Collections.Queue" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual object SyncRoot { get { if (this._syncRoot == null) { Interlocked.CompareExchange(ref this._syncRoot, new object(), null); } return this._syncRoot; } } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Queue" /> class that is empty, has the default initial capacity, and uses the default growth factor. /// </summary> public Queue() : this(32, 2f) { } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Queue" /> class that is empty, has the specified initial capacity, and uses the default growth factor. /// </summary> /// <param name="capacity"> /// The initial number of elements that the <see cref="T:System.Collections.Queue" /> can contain. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="capacity" /> is less than zero. /// </exception> public Queue(int capacity) : this(capacity, 2f) { } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Queue" /> class that is empty, has the specified initial capacity, and uses the specified growth factor. /// </summary> /// <param name="capacity"> /// The initial number of elements that the <see cref="T:System.Collections.Queue" /> can contain. /// </param> /// <param name="growFactor"> /// The factor by which the capacity of the <see cref="T:System.Collections.Queue" /> is expanded. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="capacity" /> is less than zero. /// /// -or- /// <paramref name="growFactor" /> is less than 1.0 or greater than 10.0. /// </exception> public Queue(int capacity, float growFactor) { if (capacity < 0) { throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if ((double)growFactor < 1.0 || (double)growFactor > 10.0) { throw new ArgumentOutOfRangeException("growFactor", Environment.GetResourceString("ArgumentOutOfRange_QueueGrowFactor", new object[] { 1, 10 })); } this._array = new object[capacity]; this._head = 0; this._tail = 0; this._size = 0; this._growFactor = (int)(growFactor * 100f); } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Queue" /> class that contains elements copied from the specified collection, has the same initial capacity as the number of elements copied, and uses the default growth factor. /// </summary> /// <param name="col"> /// The <see cref="T:System.Collections.ICollection" /> to copy elements from. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="col" /> is null. /// </exception> public Queue(ICollection col) : this((col == null) ? 32 : col.Count) { if (col == null) { throw new ArgumentNullException("col"); } IEnumerator enumerator = col.GetEnumerator(); while (enumerator.MoveNext()) { this.Enqueue(enumerator.Current); } } /// <summary> /// Creates a shallow copy of the <see cref="T:System.Collections.Queue" />. /// </summary> /// <returns> /// A shallow copy of the <see cref="T:System.Collections.Queue" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual object Clone() { Queue queue = new Queue(this._size); queue._size = this._size; int num = this._size; int num2 = (this._array.Length - this._head < num) ? (this._array.Length - this._head) : num; Array.Copy(this._array, this._head, queue._array, 0, num2); num -= num2; if (num > 0) { Array.Copy(this._array, 0, queue._array, this._array.Length - this._head, num); } queue._version = this._version; return queue; } /// <summary> /// Removes all objects from the <see cref="T:System.Collections.Queue" />. /// </summary> /// <filterpriority>2</filterpriority> public virtual void Clear() { if (this._head < this._tail) { Array.Clear(this._array, this._head, this._size); } else { Array.Clear(this._array, this._head, this._array.Length - this._head); Array.Clear(this._array, 0, this._tail); } this._head = 0; this._tail = 0; this._size = 0; this._version++; } /// <summary> /// Copies the <see cref="T:System.Collections.Queue" /> elements to an existing one-dimensional <see cref="T:System.Array" />, starting at the specified array index. /// </summary> /// <param name="array"> /// The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.Queue" />. The <see cref="T:System.Array" /> must have zero-based indexing. /// </param> /// <param name="index"> /// The zero-based index in <paramref name="array" /> at which copying begins. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="array" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="array" /> is multidimensional. /// /// -or- /// <paramref name="index" /> is equal to or greater than the length of <paramref name="array" />. /// /// -or- /// /// The number of elements in the source <see cref="T:System.Collections.Queue" /> is greater than the available space from <paramref name="index" /> to the end of the destination <paramref name="array" />. /// </exception> /// <exception cref="T:System.ArrayTypeMismatchException"> /// The type of the source <see cref="T:System.Collections.Queue" /> cannot be cast automatically to the type of the destination <paramref name="array" />. /// </exception> /// <filterpriority>2</filterpriority> public virtual void CopyTo(Array array, int index) { if (array == null) { throw new ArgumentNullException("array"); } if (array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } if (index < 0) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } int length = array.Length; if (length - index < this._size) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } int num = this._size; if (num == 0) { return; } int num2 = (this._array.Length - this._head < num) ? (this._array.Length - this._head) : num; Array.Copy(this._array, this._head, array, index, num2); num -= num2; if (num > 0) { Array.Copy(this._array, 0, array, index + this._array.Length - this._head, num); } } /// <summary> /// Adds an object to the end of the <see cref="T:System.Collections.Queue" />. /// </summary> /// <param name="obj"> /// The object to add to the <see cref="T:System.Collections.Queue" />. The value can be null. /// </param> /// <filterpriority>2</filterpriority> public virtual void Enqueue(object obj) { if (this._size == this._array.Length) { int num = (int)((long)this._array.Length * (long)this._growFactor / 100L); if (num < this._array.Length + 4) { num = this._array.Length + 4; } this.SetCapacity(num); } this._array[this._tail] = obj; this._tail = (this._tail + 1) % this._array.Length; this._size++; this._version++; } /// <summary> /// Returns an enumerator that iterates through the <see cref="T:System.Collections.Queue" />. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IEnumerator" /> for the <see cref="T:System.Collections.Queue" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual IEnumerator GetEnumerator() { return new Queue.QueueEnumerator(this); } /// <summary> /// Removes and returns the object at the beginning of the <see cref="T:System.Collections.Queue" />. /// </summary> /// <returns> /// The object that is removed from the beginning of the <see cref="T:System.Collections.Queue" />. /// </returns> /// <exception cref="T:System.InvalidOperationException"> /// The <see cref="T:System.Collections.Queue" /> is empty. /// </exception> /// <filterpriority>2</filterpriority> public virtual object Dequeue() { if (this._size == 0) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EmptyQueue")); } object result = this._array[this._head]; this._array[this._head] = null; this._head = (this._head + 1) % this._array.Length; this._size--; this._version++; return result; } /// <summary> /// Returns the object at the beginning of the <see cref="T:System.Collections.Queue" /> without removing it. /// </summary> /// <returns> /// The object at the beginning of the <see cref="T:System.Collections.Queue" />. /// </returns> /// <exception cref="T:System.InvalidOperationException"> /// The <see cref="T:System.Collections.Queue" /> is empty. /// </exception> /// <filterpriority>2</filterpriority> public virtual object Peek() { if (this._size == 0) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EmptyQueue")); } return this._array[this._head]; } /// <summary> /// Returns a <see cref="T:System.Collections.Queue" /> wrapper that is synchronized (thread safe). /// </summary> /// <returns> /// A <see cref="T:System.Collections.Queue" /> wrapper that is synchronized (thread safe). /// </returns> /// <param name="queue"> /// The <see cref="T:System.Collections.Queue" /> to synchronize. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="queue" /> is null. /// </exception> /// <filterpriority>2</filterpriority> [HostProtection(SecurityAction.LinkDemand, Synchronization = true)] public static Queue Synchronized(Queue queue) { if (queue == null) { throw new ArgumentNullException("queue"); } return new Queue.SynchronizedQueue(queue); } /// <summary> /// Determines whether an element is in the <see cref="T:System.Collections.Queue" />. /// </summary> /// <returns>true if <paramref name="obj" /> is found in the <see cref="T:System.Collections.Queue" />; otherwise, false. /// </returns> /// <param name="obj"> /// The <see cref="T:System.Object" /> to locate in the <see cref="T:System.Collections.Queue" />. The value can be null. /// </param> /// <filterpriority>2</filterpriority> public virtual bool Contains(object obj) { int num = this._head; int size = this._size; while (size-- > 0) { if (obj == null) { if (this._array[num] == null) { return true; } } else { if (this._array[num] != null && this._array[num].Equals(obj)) { return true; } } num = (num + 1) % this._array.Length; } return false; } internal object GetElement(int i) { return this._array[(this._head + i) % this._array.Length]; } /// <summary> /// Copies the <see cref="T:System.Collections.Queue" /> elements to a new array. /// </summary> /// <returns> /// A new array containing elements copied from the <see cref="T:System.Collections.Queue" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual object[] ToArray() { object[] array = new object[this._size]; if (this._size == 0) { return array; } if (this._head < this._tail) { Array.Copy(this._array, this._head, array, 0, this._size); } else { Array.Copy(this._array, this._head, array, 0, this._array.Length - this._head); Array.Copy(this._array, 0, array, this._array.Length - this._head, this._tail); } return array; } private void SetCapacity(int capacity) { object[] array = new object[capacity]; if (this._size > 0) { if (this._head < this._tail) { Array.Copy(this._array, this._head, array, 0, this._size); } else { Array.Copy(this._array, this._head, array, 0, this._array.Length - this._head); Array.Copy(this._array, 0, array, this._array.Length - this._head, this._tail); } } this._array = array; this._head = 0; this._tail = ((this._size == capacity) ? 0 : this._size); this._version++; } /// <summary> /// Sets the capacity to the actual number of elements in the <see cref="T:System.Collections.Queue" />. /// </summary> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.Queue" /> is read-only. /// </exception> /// <filterpriority>2</filterpriority> public virtual void TrimToSize() { this.SetCapacity(this._size); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Threading; namespace System.Collections { /// <summary> /// Represents a simple last-in-first-out (LIFO) non-generic collection of objects. /// </summary> /// <filterpriority>1</filterpriority> [DebuggerDisplay("Count = {Count}"), DebuggerTypeProxy(typeof(Stack.StackDebugView)), ComVisible(true)] [Serializable] public class Stack : ICollection, IEnumerable, ICloneable { [Serializable] private class SyncStack : Stack { private Stack _s; private object _root; public override bool IsSynchronized { get { return true; } } public override object SyncRoot { get { return this._root; } } public override int Count { get { object root; Monitor.Enter(root = this._root); int count; try { count = this._s.Count; } finally { Monitor.Exit(root); } return count; } } internal SyncStack(Stack stack) { this._s = stack; this._root = stack.SyncRoot; } public override bool Contains(object obj) { object root; Monitor.Enter(root = this._root); bool result; try { result = this._s.Contains(obj); } finally { Monitor.Exit(root); } return result; } public override object Clone() { object root; Monitor.Enter(root = this._root); object result; try { result = new Stack.SyncStack((Stack)this._s.Clone()); } finally { Monitor.Exit(root); } return result; } public override void Clear() { object root; Monitor.Enter(root = this._root); try { this._s.Clear(); } finally { Monitor.Exit(root); } } public override void CopyTo(Array array, int arrayIndex) { object root; Monitor.Enter(root = this._root); try { this._s.CopyTo(array, arrayIndex); } finally { Monitor.Exit(root); } } public override void Push(object value) { object root; Monitor.Enter(root = this._root); try { this._s.Push(value); } finally { Monitor.Exit(root); } } public override object Pop() { object root; Monitor.Enter(root = this._root); object result; try { result = this._s.Pop(); } finally { Monitor.Exit(root); } return result; } public override IEnumerator GetEnumerator() { object root; Monitor.Enter(root = this._root); IEnumerator enumerator; try { enumerator = this._s.GetEnumerator(); } finally { Monitor.Exit(root); } return enumerator; } public override object Peek() { object root; Monitor.Enter(root = this._root); object result; try { result = this._s.Peek(); } finally { Monitor.Exit(root); } return result; } public override object[] ToArray() { object root; Monitor.Enter(root = this._root); object[] result; try { result = this._s.ToArray(); } finally { Monitor.Exit(root); } return result; } } [Serializable] private class StackEnumerator : IEnumerator, ICloneable { private Stack _stack; private int _index; private int _version; private object currentElement; public virtual object Current { get { if (this._index == -2) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted")); } if (this._index == -1) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded")); } return this.currentElement; } } internal StackEnumerator(Stack stack) { this._stack = stack; this._version = this._stack._version; this._index = -2; this.currentElement = null; } public object Clone() { return base.MemberwiseClone(); } public virtual bool MoveNext() { if (this._version != this._stack._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } bool flag; if (this._index == -2) { this._index = this._stack._size - 1; flag = (this._index >= 0); if (flag) { this.currentElement = this._stack._array[this._index]; } return flag; } if (this._index == -1) { return false; } flag = (--this._index >= 0); if (flag) { this.currentElement = this._stack._array[this._index]; } else { this.currentElement = null; } return flag; } public virtual void Reset() { if (this._version != this._stack._version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } this._index = -2; this.currentElement = null; } } internal class StackDebugView { private Stack stack; [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public object[] Items { get { return this.stack.ToArray(); } } public StackDebugView(Stack stack) { if (stack == null) { throw new ArgumentNullException("stack"); } this.stack = stack; } } private const int _defaultCapacity = 10; private object[] _array; private int _size; private int _version; [NonSerialized] private object _syncRoot; /// <summary> /// Gets the number of elements contained in the <see cref="T:System.Collections.Stack" />. /// </summary> /// <returns> /// The number of elements contained in the <see cref="T:System.Collections.Stack" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual int Count { get { return this._size; } } /// <summary> /// Gets a value indicating whether access to the <see cref="T:System.Collections.Stack" /> is synchronized (thread safe). /// </summary> /// <returns>true, if access to the <see cref="T:System.Collections.Stack" /> is synchronized (thread safe); otherwise, false. The default is false. /// </returns> /// <filterpriority>2</filterpriority> public virtual bool IsSynchronized { get { return false; } } /// <summary> /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.Stack" />. /// </summary> /// <returns> /// An <see cref="T:System.Object" /> that can be used to synchronize access to the <see cref="T:System.Collections.Stack" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual object SyncRoot { get { if (this._syncRoot == null) { Interlocked.CompareExchange(ref this._syncRoot, new object(), null); } return this._syncRoot; } } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Stack" /> class that is empty and has the default initial capacity. /// </summary> public Stack() { this._array = new object[10]; this._size = 0; this._version = 0; } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Stack" /> class that is empty and has the specified initial capacity or the default initial capacity, whichever is greater. /// </summary> /// <param name="initialCapacity"> /// The initial number of elements that the <see cref="T:System.Collections.Stack" /> can contain. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="initialCapacity" /> is less than zero. /// </exception> public Stack(int initialCapacity) { if (initialCapacity < 0) { throw new ArgumentOutOfRangeException("initialCapacity", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (initialCapacity < 10) { initialCapacity = 10; } this._array = new object[initialCapacity]; this._size = 0; this._version = 0; } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Stack" /> class that contains elements copied from the specified collection and has the same initial capacity as the number of elements copied. /// </summary> /// <param name="col"> /// The <see cref="T:System.Collections.ICollection" /> to copy elements from. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="col" /> is null. /// </exception> public Stack(ICollection col) : this((col == null) ? 32 : col.Count) { if (col == null) { throw new ArgumentNullException("col"); } IEnumerator enumerator = col.GetEnumerator(); while (enumerator.MoveNext()) { this.Push(enumerator.Current); } } /// <summary> /// Removes all objects from the <see cref="T:System.Collections.Stack" />. /// </summary> /// <filterpriority>2</filterpriority> public virtual void Clear() { Array.Clear(this._array, 0, this._size); this._size = 0; this._version++; } /// <summary> /// Creates a shallow copy of the <see cref="T:System.Collections.Stack" />. /// </summary> /// <returns> /// A shallow copy of the <see cref="T:System.Collections.Stack" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual object Clone() { Stack stack = new Stack(this._size); stack._size = this._size; Array.Copy(this._array, 0, stack._array, 0, this._size); stack._version = this._version; return stack; } /// <summary> /// Determines whether an element is in the <see cref="T:System.Collections.Stack" />. /// </summary> /// <returns>true, if <paramref name="obj" /> is found in the <see cref="T:System.Collections.Stack" />; otherwise, false. /// </returns> /// <param name="obj"> /// The <see cref="T:System.Object" /> to locate in the <see cref="T:System.Collections.Stack" />. The value can be null. /// </param> /// <filterpriority>2</filterpriority> public virtual bool Contains(object obj) { int size = this._size; while (size-- > 0) { if (obj == null) { if (this._array[size] == null) { return true; } } else { if (this._array[size] != null && this._array[size].Equals(obj)) { return true; } } } return false; } /// <summary> /// Copies the <see cref="T:System.Collections.Stack" /> to an existing one-dimensional <see cref="T:System.Array" />, starting at the specified array index. /// </summary> /// <param name="array"> /// The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.Stack" />. The <see cref="T:System.Array" /> must have zero-based indexing. /// </param> /// <param name="index"> /// The zero-based index in <paramref name="array" /> at which copying begins. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="array" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="array" /> is multidimensional. /// /// -or- /// <paramref name="index" /> is equal to or greater than the length of <paramref name="array" />. /// /// -or- /// /// The number of elements in the source <see cref="T:System.Collections.Stack" /> is greater than the available space from <paramref name="index" /> to the end of the destination <paramref name="array" />. /// </exception> /// <exception cref="T:System.InvalidCastException"> /// The type of the source <see cref="T:System.Collections.Stack" /> cannot be cast automatically to the type of the destination <paramref name="array" />. /// </exception> /// <filterpriority>2</filterpriority> public virtual void CopyTo(Array array, int index) { if (array == null) { throw new ArgumentNullException("array"); } if (array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } if (index < 0) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (array.Length - index < this._size) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } int i = 0; if (array is object[]) { object[] array2 = (object[])array; while (i < this._size) { array2[i + index] = this._array[this._size - i - 1]; i++; } return; } while (i < this._size) { array.SetValue(this._array[this._size - i - 1], i + index); i++; } } /// <summary> /// Returns an <see cref="T:System.Collections.IEnumerator" /> for the <see cref="T:System.Collections.Stack" />. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IEnumerator" /> for the <see cref="T:System.Collections.Stack" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual IEnumerator GetEnumerator() { return new Stack.StackEnumerator(this); } /// <summary> /// Returns the object at the top of the <see cref="T:System.Collections.Stack" /> without removing it. /// </summary> /// <returns> /// The <see cref="T:System.Object" /> at the top of the <see cref="T:System.Collections.Stack" />. /// </returns> /// <exception cref="T:System.InvalidOperationException"> /// The <see cref="T:System.Collections.Stack" /> is empty. /// </exception> /// <filterpriority>2</filterpriority> public virtual object Peek() { if (this._size == 0) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EmptyStack")); } return this._array[this._size - 1]; } /// <summary> /// Removes and returns the object at the top of the <see cref="T:System.Collections.Stack" />. /// </summary> /// <returns> /// The <see cref="T:System.Object" /> removed from the top of the <see cref="T:System.Collections.Stack" />. /// </returns> /// <exception cref="T:System.InvalidOperationException"> /// The <see cref="T:System.Collections.Stack" /> is empty. /// </exception> /// <filterpriority>2</filterpriority> public virtual object Pop() { if (this._size == 0) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EmptyStack")); } this._version++; object result = this._array[--this._size]; this._array[this._size] = null; return result; } /// <summary> /// Inserts an object at the top of the <see cref="T:System.Collections.Stack" />. /// </summary> /// <param name="obj"> /// The <see cref="T:System.Object" /> to push onto the <see cref="T:System.Collections.Stack" />. The value can be null. /// </param> /// <filterpriority>2</filterpriority> public virtual void Push(object obj) { if (this._size == this._array.Length) { object[] array = new object[2 * this._array.Length]; Array.Copy(this._array, 0, array, 0, this._size); this._array = array; } this._array[this._size++] = obj; this._version++; } /// <summary> /// Returns a synchronized (thread safe) wrapper for the <see cref="T:System.Collections.Stack" />. /// </summary> /// <returns> /// A synchronized wrapper around the <see cref="T:System.Collections.Stack" />. /// </returns> /// <param name="stack"> /// The <see cref="T:System.Collections.Stack" /> to synchronize. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="stack" /> is null. /// </exception> /// <filterpriority>2</filterpriority> [HostProtection(SecurityAction.LinkDemand, Synchronization = true)] public static Stack Synchronized(Stack stack) { if (stack == null) { throw new ArgumentNullException("stack"); } return new Stack.SyncStack(stack); } /// <summary> /// Copies the <see cref="T:System.Collections.Stack" /> to a new array. /// </summary> /// <returns> /// A new array containing copies of the elements of the <see cref="T:System.Collections.Stack" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual object[] ToArray() { object[] array = new object[this._size]; for (int i = 0; i < this._size; i++) { array[i] = this._array[this._size - i - 1]; } return array; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Diagnostics; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Security.Permissions; using System.Threading; namespace System.Collections { /// <summary> /// Represents a collection of key/value pairs that are organized based on the hash code of the key. /// </summary> /// <filterpriority>1</filterpriority> [DebuggerDisplay("Count = {Count}"), DebuggerTypeProxy(typeof(Hashtable.HashtableDebugView)), ComVisible(true)] [Serializable] public class Hashtable : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable { private struct bucket { public object key; public object val; public int hash_coll; } [Serializable] private class KeyCollection : ICollection, IEnumerable { private Hashtable _hashtable; public virtual bool IsSynchronized { get { return this._hashtable.IsSynchronized; } } public virtual object SyncRoot { get { return this._hashtable.SyncRoot; } } public virtual int Count { get { return this._hashtable.count; } } internal KeyCollection(Hashtable hashtable) { this._hashtable = hashtable; } public virtual void CopyTo(Array array, int arrayIndex) { if (array == null) { throw new ArgumentNullException("array"); } if (array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } if (arrayIndex < 0) { throw new ArgumentOutOfRangeException("arrayIndex", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (array.Length - arrayIndex < this._hashtable.count) { throw new ArgumentException(Environment.GetResourceString("Arg_ArrayPlusOffTooSmall")); } this._hashtable.CopyKeys(array, arrayIndex); } public virtual IEnumerator GetEnumerator() { return new Hashtable.HashtableEnumerator(this._hashtable, 1); } } [Serializable] private class ValueCollection : ICollection, IEnumerable { private Hashtable _hashtable; public virtual bool IsSynchronized { get { return this._hashtable.IsSynchronized; } } public virtual object SyncRoot { get { return this._hashtable.SyncRoot; } } public virtual int Count { get { return this._hashtable.count; } } internal ValueCollection(Hashtable hashtable) { this._hashtable = hashtable; } public virtual void CopyTo(Array array, int arrayIndex) { if (array == null) { throw new ArgumentNullException("array"); } if (array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } if (arrayIndex < 0) { throw new ArgumentOutOfRangeException("arrayIndex", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (array.Length - arrayIndex < this._hashtable.count) { throw new ArgumentException(Environment.GetResourceString("Arg_ArrayPlusOffTooSmall")); } this._hashtable.CopyValues(array, arrayIndex); } public virtual IEnumerator GetEnumerator() { return new Hashtable.HashtableEnumerator(this._hashtable, 2); } } [Serializable] private class SyncHashtable : Hashtable { protected Hashtable _table; public override int Count { get { return this._table.Count; } } public override bool IsReadOnly { get { return this._table.IsReadOnly; } } public override bool IsFixedSize { get { return this._table.IsFixedSize; } } public override bool IsSynchronized { get { return true; } } public override object this[object key] { get { return this._table[key]; } set { object syncRoot; Monitor.Enter(syncRoot = this._table.SyncRoot); try { this._table[key] = value; } finally { Monitor.Exit(syncRoot); } } } public override object SyncRoot { get { return this._table.SyncRoot; } } public override ICollection Keys { get { object syncRoot; Monitor.Enter(syncRoot = this._table.SyncRoot); ICollection keys; try { keys = this._table.Keys; } finally { Monitor.Exit(syncRoot); } return keys; } } public override ICollection Values { get { object syncRoot; Monitor.Enter(syncRoot = this._table.SyncRoot); ICollection values; try { values = this._table.Values; } finally { Monitor.Exit(syncRoot); } return values; } } internal SyncHashtable(Hashtable table) : base(false) { this._table = table; } internal SyncHashtable(SerializationInfo info, StreamingContext context) : base(info, context) { this._table = (Hashtable)info.GetValue("ParentTable", typeof(Hashtable)); if (this._table == null) { throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState")); } } public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException("info"); } info.AddValue("ParentTable", this._table, typeof(Hashtable)); } public override void Add(object key, object value) { object syncRoot; Monitor.Enter(syncRoot = this._table.SyncRoot); try { this._table.Add(key, value); } finally { Monitor.Exit(syncRoot); } } public override void Clear() { object syncRoot; Monitor.Enter(syncRoot = this._table.SyncRoot); try { this._table.Clear(); } finally { Monitor.Exit(syncRoot); } } public override bool Contains(object key) { return this._table.Contains(key); } public override bool ContainsKey(object key) { return this._table.ContainsKey(key); } public override bool ContainsValue(object key) { object syncRoot; Monitor.Enter(syncRoot = this._table.SyncRoot); bool result; try { result = this._table.ContainsValue(key); } finally { Monitor.Exit(syncRoot); } return result; } public override void CopyTo(Array array, int arrayIndex) { object syncRoot; Monitor.Enter(syncRoot = this._table.SyncRoot); try { this._table.CopyTo(array, arrayIndex); } finally { Monitor.Exit(syncRoot); } } public override object Clone() { object syncRoot; Monitor.Enter(syncRoot = this._table.SyncRoot); object result; try { result = Hashtable.Synchronized((Hashtable)this._table.Clone()); } finally { Monitor.Exit(syncRoot); } return result; } public override IDictionaryEnumerator GetEnumerator() { return this._table.GetEnumerator(); } public override void Remove(object key) { object syncRoot; Monitor.Enter(syncRoot = this._table.SyncRoot); try { this._table.Remove(key); } finally { Monitor.Exit(syncRoot); } } public override void OnDeserialization(object sender) { } internal override KeyValuePairs[] ToKeyValuePairsArray() { return this._table.ToKeyValuePairsArray(); } } [Serializable] private class HashtableEnumerator : IDictionaryEnumerator, IEnumerator, ICloneable { internal const int Keys = 1; internal const int Values = 2; internal const int DictEntry = 3; private Hashtable hashtable; private int bucket; private int version; private bool current; private int getObjectRetType; private object currentKey; private object currentValue; public virtual object Key { get { if (!this.current) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted")); } return this.currentKey; } } public virtual DictionaryEntry Entry { get { if (!this.current) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen")); } return new DictionaryEntry(this.currentKey, this.currentValue); } } public virtual object Current { get { if (!this.current) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen")); } if (this.getObjectRetType == 1) { return this.currentKey; } if (this.getObjectRetType == 2) { return this.currentValue; } return new DictionaryEntry(this.currentKey, this.currentValue); } } public virtual object Value { get { if (!this.current) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen")); } return this.currentValue; } } internal HashtableEnumerator(Hashtable hashtable, int getObjRetType) { this.hashtable = hashtable; this.bucket = hashtable.buckets.Length; this.version = hashtable.version; this.current = false; this.getObjectRetType = getObjRetType; } public object Clone() { return base.MemberwiseClone(); } public virtual bool MoveNext() { if (this.version != this.hashtable.version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } while (this.bucket > 0) { this.bucket--; object key = this.hashtable.buckets[this.bucket].key; if (key != null && key != this.hashtable.buckets) { this.currentKey = key; this.currentValue = this.hashtable.buckets[this.bucket].val; this.current = true; return true; } } this.current = false; return false; } public virtual void Reset() { if (this.version != this.hashtable.version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } this.current = false; this.bucket = this.hashtable.buckets.Length; this.currentKey = null; this.currentValue = null; } } internal class HashtableDebugView { private Hashtable hashtable; [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public KeyValuePairs[] Items { get { return this.hashtable.ToKeyValuePairsArray(); } } public HashtableDebugView(Hashtable hashtable) { if (hashtable == null) { throw new ArgumentNullException("hashtable"); } this.hashtable = hashtable; } } private const string LoadFactorName = "LoadFactor"; private const string VersionName = "Version"; private const string ComparerName = "Comparer"; private const string HashCodeProviderName = "HashCodeProvider"; private const string HashSizeName = "HashSize"; private const string KeysName = "Keys"; private const string ValuesName = "Values"; private const string KeyComparerName = "KeyComparer"; private Hashtable.bucket[] buckets; private int count; private int occupancy; private int loadsize; private float loadFactor; private volatile int version; private volatile bool isWriterInProgress; private ICollection keys; private ICollection values; private IEqualityComparer _keycomparer; private object _syncRoot; private SerializationInfo m_siInfo; /// <summary> /// Gets or sets the object that can dispense hash codes. /// </summary> /// <returns> /// The object that can dispense hash codes. /// </returns> /// <exception cref="T:System.ArgumentException"> /// The property is set to a value, but the hash table was created using an <see cref="T:System.Collections.IEqualityComparer" />. /// </exception> [Obsolete("Please use EqualityComparer property.")] protected IHashCodeProvider hcp { get { if (this._keycomparer is CompatibleComparer) { return ((CompatibleComparer)this._keycomparer).HashCodeProvider; } if (this._keycomparer == null) { return null; } throw new ArgumentException(Environment.GetResourceString("Arg_CannotMixComparisonInfrastructure")); } set { if (this._keycomparer is CompatibleComparer) { CompatibleComparer compatibleComparer = (CompatibleComparer)this._keycomparer; this._keycomparer = new CompatibleComparer(compatibleComparer.Comparer, value); return; } if (this._keycomparer == null) { this._keycomparer = new CompatibleComparer(null, value); return; } throw new ArgumentException(Environment.GetResourceString("Arg_CannotMixComparisonInfrastructure")); } } /// <summary> /// Gets or sets the <see cref="T:System.Collections.IComparer" /> to use for the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <returns> /// The <see cref="T:System.Collections.IComparer" /> to use for the <see cref="T:System.Collections.Hashtable" />. /// </returns> /// <exception cref="T:System.ArgumentException"> /// The property is set to a value, but the hash table was created using an <see cref="T:System.Collections.IEqualityComparer" />. /// </exception> [Obsolete("Please use KeyComparer properties.")] protected IComparer comparer { get { if (this._keycomparer is CompatibleComparer) { return ((CompatibleComparer)this._keycomparer).Comparer; } if (this._keycomparer == null) { return null; } throw new ArgumentException(Environment.GetResourceString("Arg_CannotMixComparisonInfrastructure")); } set { if (this._keycomparer is CompatibleComparer) { CompatibleComparer compatibleComparer = (CompatibleComparer)this._keycomparer; this._keycomparer = new CompatibleComparer(value, compatibleComparer.HashCodeProvider); return; } if (this._keycomparer == null) { this._keycomparer = new CompatibleComparer(value, null); return; } throw new ArgumentException(Environment.GetResourceString("Arg_CannotMixComparisonInfrastructure")); } } /// <summary> /// Gets the <see cref="T:System.Collections.IEqualityComparer" /> to use for the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <returns> /// The <see cref="T:System.Collections.IEqualityComparer" /> to use for the <see cref="T:System.Collections.Hashtable" />. /// </returns> /// <exception cref="T:System.ArgumentException"> /// The property is set to a value, but the hash table was created using an <see cref="T:System.Collections.IHashCodeProvider" /> and an <see cref="T:System.Collections.IComparer" />. /// </exception> protected IEqualityComparer EqualityComparer { get { return this._keycomparer; } } /// <summary> /// Gets or sets the value associated with the specified key. /// </summary> /// <returns> /// The value associated with the specified key. If the specified key is not found, attempting to get it returns null, and attempting to set it creates a new element using the specified key. /// </returns> /// <param name="key"> /// The key whose value to get or set. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The property is set and the <see cref="T:System.Collections.Hashtable" /> is read-only. /// /// -or- /// /// The property is set, <paramref name="key" /> does not exist in the collection, and the <see cref="T:System.Collections.Hashtable" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> public virtual object this[object key] { get { if (key == null) { throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); } Hashtable.bucket[] array = this.buckets; uint num2; uint num3; uint num = this.InitHash(key, array.Length, out num2, out num3); int num4 = 0; int num5 = (int)(num2 % (uint)array.Length); Hashtable.bucket bucket; while (true) { int num6 = 0; int num7; do { num7 = this.version; bucket = array[num5]; if (++num6 % 8 == 0) { Thread.Sleep(1); } } while (this.isWriterInProgress || num7 != this.version); if (bucket.key == null) { break; } if ((long)(bucket.hash_coll & 2147483647) == (long)((ulong)num) && this.KeyEquals(bucket.key, key)) { goto Block_7; } num5 = (int)(((long)num5 + (long)((ulong)num3)) % (long)((ulong)array.Length)); if (bucket.hash_coll >= 0 || ++num4 >= array.Length) { goto IL_D7; } } return null; Block_7: return bucket.val; IL_D7: return null; } set { this.Insert(key, value, false); } } /// <summary> /// Gets a value indicating whether the <see cref="T:System.Collections.Hashtable" /> is read-only. /// </summary> /// <returns>true if the <see cref="T:System.Collections.Hashtable" /> is read-only; otherwise, false. The default is false. /// </returns> /// <filterpriority>2</filterpriority> public virtual bool IsReadOnly { get { return false; } } /// <summary> /// Gets a value indicating whether the <see cref="T:System.Collections.Hashtable" /> has a fixed size. /// </summary> /// <returns>true if the <see cref="T:System.Collections.Hashtable" /> has a fixed size; otherwise, false. The default is false. /// </returns> /// <filterpriority>2</filterpriority> public virtual bool IsFixedSize { get { return false; } } /// <summary> /// Gets a value indicating whether access to the <see cref="T:System.Collections.Hashtable" /> is synchronized (thread safe). /// </summary> /// <returns>true if access to the <see cref="T:System.Collections.Hashtable" /> is synchronized (thread safe); otherwise, false. The default is false. /// </returns> /// <filterpriority>2</filterpriority> public virtual bool IsSynchronized { get { return false; } } /// <summary> /// Gets an <see cref="T:System.Collections.ICollection" /> containing the keys in the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <returns> /// An <see cref="T:System.Collections.ICollection" /> containing the keys in the <see cref="T:System.Collections.Hashtable" />. /// </returns> /// <filterpriority>1</filterpriority> public virtual ICollection Keys { get { if (this.keys == null) { this.keys = new Hashtable.KeyCollection(this); } return this.keys; } } /// <summary> /// Gets an <see cref="T:System.Collections.ICollection" /> containing the values in the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <returns> /// An <see cref="T:System.Collections.ICollection" /> containing the values in the <see cref="T:System.Collections.Hashtable" />. /// </returns> /// <filterpriority>1</filterpriority> public virtual ICollection Values { get { if (this.values == null) { this.values = new Hashtable.ValueCollection(this); } return this.values; } } /// <summary> /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <returns> /// An object that can be used to synchronize access to the <see cref="T:System.Collections.Hashtable" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual object SyncRoot { get { if (this._syncRoot == null) { Interlocked.CompareExchange(ref this._syncRoot, new object(), null); } return this._syncRoot; } } /// <summary> /// Gets the number of key/value pairs contained in the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <returns> /// The number of key/value pairs contained in the <see cref="T:System.Collections.Hashtable" />. /// </returns> /// <filterpriority>1</filterpriority> public virtual int Count { get { return this.count; } } internal Hashtable(bool trash) { } /// <summary> /// Initializes a new, empty instance of the <see cref="T:System.Collections.Hashtable" /> class using the default initial capacity, load factor, hash code provider, and comparer. /// </summary> public Hashtable() : this(0, 1f) { } /// <summary> /// Initializes a new, empty instance of the <see cref="T:System.Collections.Hashtable" /> class using the specified initial capacity, and the default load factor, hash code provider, and comparer. /// </summary> /// <param name="capacity"> /// The approximate number of elements that the <see cref="T:System.Collections.Hashtable" /> object can initially contain. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="capacity" /> is less than zero. /// </exception> public Hashtable(int capacity) : this(capacity, 1f) { } /// <summary> /// Initializes a new, empty instance of the <see cref="T:System.Collections.Hashtable" /> class using the specified initial capacity and load factor, and the default hash code provider and comparer. /// </summary> /// <param name="capacity"> /// The approximate number of elements that the <see cref="T:System.Collections.Hashtable" /> object can initially contain. /// </param> /// <param name="loadFactor"> /// A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. The result is the maximum ratio of elements to buckets. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="capacity" /> is less than zero. /// /// -or- /// <paramref name="loadFactor" /> is less than 0.1. /// /// -or- /// <paramref name="loadFactor" /> is greater than 1.0. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="capacity" /> is causing an overflow. /// </exception> public Hashtable(int capacity, float loadFactor) { if (capacity < 0) { throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (loadFactor < 0.1f || loadFactor > 1f) { throw new ArgumentOutOfRangeException("loadFactor", Environment.GetResourceString("ArgumentOutOfRange_HashtableLoadFactor", new object[] { 0.1, 1.0 })); } this.loadFactor = 0.72f * loadFactor; double num = (double)((float)capacity / this.loadFactor); if (num > 2147483647.0) { throw new ArgumentException(Environment.GetResourceString("Arg_HTCapacityOverflow")); } int num2 = (num > 11.0) ? HashHelpers.GetPrime((int)num) : 11; this.buckets = new Hashtable.bucket[num2]; this.loadsize = (int)(this.loadFactor * (float)num2); this.isWriterInProgress = false; } /// <summary> /// Initializes a new, empty instance of the <see cref="T:System.Collections.Hashtable" /> class using the specified initial capacity, load factor, hash code provider, and comparer. /// </summary> /// <param name="capacity"> /// The approximate number of elements that the <see cref="T:System.Collections.Hashtable" /> object can initially contain. /// </param> /// <param name="loadFactor"> /// A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. The result is the maximum ratio of elements to buckets. /// </param> /// <param name="hcp"> /// The <see cref="T:System.Collections.IHashCodeProvider" /> object that supplies the hash codes for all keys in the <see cref="T:System.Collections.Hashtable" />. /// /// -or- /// null to use the default hash code provider, which is each key's implementation of <see cref="M:System.Object.GetHashCode" />. /// </param> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> object to use to determine whether two keys are equal. /// /// -or- /// null to use the default comparer, which is each key's implementation of <see cref="M:System.Object.Equals(System.Object)" />. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="capacity" /> is less than zero. /// /// -or- /// <paramref name="loadFactor" /> is less than 0.1. /// /// -or- /// <paramref name="loadFactor" /> is greater than 1.0. /// </exception> [Obsolete("Please use Hashtable(int, float, IEqualityComparer) instead.")] public Hashtable(int capacity, float loadFactor, IHashCodeProvider hcp, IComparer comparer) : this(capacity, loadFactor) { if (hcp == null && comparer == null) { this._keycomparer = null; return; } this._keycomparer = new CompatibleComparer(comparer, hcp); } /// <summary> /// Initializes a new, empty instance of the <see cref="T:System.Collections.Hashtable" /> class using the specified initial capacity, load factor, and <see cref="T:System.Collections.IEqualityComparer" /> object. /// </summary> /// <param name="capacity"> /// The approximate number of elements that the <see cref="T:System.Collections.Hashtable" /> object can initially contain. /// </param> /// <param name="loadFactor"> /// A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. The result is the maximum ratio of elements to buckets. /// </param> /// <param name="equalityComparer"> /// The <see cref="T:System.Collections.IEqualityComparer" /> object that defines the hash code provider and the comparer to use with the <see cref="T:System.Collections.Hashtable" />. /// /// -or- /// null to use the default hash code provider and the default comparer. The default hash code provider is each key's implementation of <see cref="M:System.Object.GetHashCode" /> and the default comparer is each key's implementation of <see cref="M:System.Object.Equals(System.Object)" />. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="capacity" /> is less than zero. /// /// -or- /// <paramref name="loadFactor" /> is less than 0.1. /// /// -or- /// <paramref name="loadFactor" /> is greater than 1.0. /// </exception> public Hashtable(int capacity, float loadFactor, IEqualityComparer equalityComparer) : this(capacity, loadFactor) { this._keycomparer = equalityComparer; } /// <summary> /// Initializes a new, empty instance of the <see cref="T:System.Collections.Hashtable" /> class using the default initial capacity and load factor, and the specified hash code provider and comparer. /// </summary> /// <param name="hcp"> /// The <see cref="T:System.Collections.IHashCodeProvider" /> object that supplies the hash codes for all keys in the <see cref="T:System.Collections.Hashtable" /> object. /// /// -or- /// null to use the default hash code provider, which is each key's implementation of <see cref="M:System.Object.GetHashCode" />. /// </param> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> object to use to determine whether two keys are equal. /// /// -or- /// null to use the default comparer, which is each key's implementation of <see cref="M:System.Object.Equals(System.Object)" />. /// </param> [Obsolete("Please use Hashtable(IEqualityComparer) instead.")] public Hashtable(IHashCodeProvider hcp, IComparer comparer) : this(0, 1f, hcp, comparer) { } /// <summary> /// Initializes a new, empty instance of the <see cref="T:System.Collections.Hashtable" /> class using the default initial capacity and load factor, and the specified <see cref="T:System.Collections.IEqualityComparer" /> object. /// </summary> /// <param name="equalityComparer"> /// The <see cref="T:System.Collections.IEqualityComparer" /> object that defines the hash code provider and the comparer to use with the <see cref="T:System.Collections.Hashtable" /> object. /// /// -or- /// null to use the default hash code provider and the default comparer. The default hash code provider is each key's implementation of <see cref="M:System.Object.GetHashCode" /> and the default comparer is each key's implementation of <see cref="M:System.Object.Equals(System.Object)" />. /// </param> public Hashtable(IEqualityComparer equalityComparer) : this(0, 1f, equalityComparer) { } /// <summary> /// Initializes a new, empty instance of the <see cref="T:System.Collections.Hashtable" /> class using the specified initial capacity, hash code provider, comparer, and the default load factor. /// </summary> /// <param name="capacity"> /// The approximate number of elements that the <see cref="T:System.Collections.Hashtable" /> object can initially contain. /// </param> /// <param name="hcp"> /// The <see cref="T:System.Collections.IHashCodeProvider" /> object that supplies the hash codes for all keys in the <see cref="T:System.Collections.Hashtable" />. /// /// -or- /// null to use the default hash code provider, which is each key's implementation of <see cref="M:System.Object.GetHashCode" />. /// </param> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> object to use to determine whether two keys are equal. /// /// -or- /// null to use the default comparer, which is each key's implementation of <see cref="M:System.Object.Equals(System.Object)" />. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="capacity" /> is less than zero. /// </exception> [Obsolete("Please use Hashtable(int, IEqualityComparer) instead.")] public Hashtable(int capacity, IHashCodeProvider hcp, IComparer comparer) : this(capacity, 1f, hcp, comparer) { } /// <summary> /// Initializes a new, empty instance of the <see cref="T:System.Collections.Hashtable" /> class using the specified initial capacity and <see cref="T:System.Collections.IEqualityComparer" />, and the default load factor. /// </summary> /// <param name="capacity"> /// The approximate number of elements that the <see cref="T:System.Collections.Hashtable" /> object can initially contain. /// </param> /// <param name="equalityComparer"> /// The <see cref="T:System.Collections.IEqualityComparer" /> object that defines the hash code provider and the comparer to use with the <see cref="T:System.Collections.Hashtable" />. /// /// -or- /// null to use the default hash code provider and the default comparer. The default hash code provider is each key's implementation of <see cref="M:System.Object.GetHashCode" /> and the default comparer is each key's implementation of <see cref="M:System.Object.Equals(System.Object)" />. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="capacity" /> is less than zero. /// </exception> public Hashtable(int capacity, IEqualityComparer equalityComparer) : this(capacity, 1f, equalityComparer) { } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Hashtable" /> class by copying the elements from the specified dictionary to the new <see cref="T:System.Collections.Hashtable" /> object. The new <see cref="T:System.Collections.Hashtable" /> object has an initial capacity equal to the number of elements copied, and uses the default load factor, hash code provider, and comparer. /// </summary> /// <param name="d"> /// The <see cref="T:System.Collections.IDictionary" /> object to copy to a new <see cref="T:System.Collections.Hashtable" /> object. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="d" /> is null. /// </exception> public Hashtable(IDictionary d) : this(d, 1f) { } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Hashtable" /> class by copying the elements from the specified dictionary to the new <see cref="T:System.Collections.Hashtable" /> object. The new <see cref="T:System.Collections.Hashtable" /> object has an initial capacity equal to the number of elements copied, and uses the specified load factor, and the default hash code provider and comparer. /// </summary> /// <param name="d"> /// The <see cref="T:System.Collections.IDictionary" /> object to copy to a new <see cref="T:System.Collections.Hashtable" /> object. /// </param> /// <param name="loadFactor"> /// A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. The result is the maximum ratio of elements to buckets. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="d" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="loadFactor" /> is less than 0.1. /// /// -or- /// <paramref name="loadFactor" /> is greater than 1.0. /// </exception> public Hashtable(IDictionary d, float loadFactor) : this(d, loadFactor, null) { } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Hashtable" /> class by copying the elements from the specified dictionary to the new <see cref="T:System.Collections.Hashtable" /> object. The new <see cref="T:System.Collections.Hashtable" /> object has an initial capacity equal to the number of elements copied, and uses the default load factor, and the specified hash code provider and comparer. /// </summary> /// <param name="d"> /// The <see cref="T:System.Collections.IDictionary" /> object to copy to a new <see cref="T:System.Collections.Hashtable" /> object. /// </param> /// <param name="hcp"> /// The <see cref="T:System.Collections.IHashCodeProvider" /> object that supplies the hash codes for all keys in the <see cref="T:System.Collections.Hashtable" />. /// /// -or- /// null to use the default hash code provider, which is each key's implementation of <see cref="M:System.Object.GetHashCode" />. /// </param> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> object to use to determine whether two keys are equal. /// /// -or- /// null to use the default comparer, which is each key's implementation of <see cref="M:System.Object.Equals(System.Object)" />. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="d" /> is null. /// </exception> [Obsolete("Please use Hashtable(IDictionary, IEqualityComparer) instead.")] public Hashtable(IDictionary d, IHashCodeProvider hcp, IComparer comparer) : this(d, 1f, hcp, comparer) { } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Hashtable" /> class by copying the elements from the specified dictionary to a new <see cref="T:System.Collections.Hashtable" /> object. The new <see cref="T:System.Collections.Hashtable" /> object has an initial capacity equal to the number of elements copied, and uses the default load factor and the specified <see cref="T:System.Collections.IEqualityComparer" /> object. /// </summary> /// <param name="d"> /// The <see cref="T:System.Collections.IDictionary" /> object to copy to a new <see cref="T:System.Collections.Hashtable" /> object. /// </param> /// <param name="equalityComparer"> /// The <see cref="T:System.Collections.IEqualityComparer" /> object that defines the hash code provider and the comparer to use with the <see cref="T:System.Collections.Hashtable" />. /// /// -or- /// null to use the default hash code provider and the default comparer. The default hash code provider is each key's implementation of <see cref="M:System.Object.GetHashCode" /> and the default comparer is each key's implementation of <see cref="M:System.Object.Equals(System.Object)" />. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="d" /> is null. /// </exception> public Hashtable(IDictionary d, IEqualityComparer equalityComparer) : this(d, 1f, equalityComparer) { } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Hashtable" /> class by copying the elements from the specified dictionary to the new <see cref="T:System.Collections.Hashtable" /> object. The new <see cref="T:System.Collections.Hashtable" /> object has an initial capacity equal to the number of elements copied, and uses the specified load factor, hash code provider, and comparer. /// </summary> /// <param name="d"> /// The <see cref="T:System.Collections.IDictionary" /> object to copy to a new <see cref="T:System.Collections.Hashtable" /> object. /// </param> /// <param name="loadFactor"> /// A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. The result is the maximum ratio of elements to buckets. /// </param> /// <param name="hcp"> /// The <see cref="T:System.Collections.IHashCodeProvider" /> object that supplies the hash codes for all keys in the <see cref="T:System.Collections.Hashtable" />. /// /// -or- /// null to use the default hash code provider, which is each key's implementation of <see cref="M:System.Object.GetHashCode" />. /// </param> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> object to use to determine whether two keys are equal. /// /// -or- /// null to use the default comparer, which is each key's implementation of <see cref="M:System.Object.Equals(System.Object)" />. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="d" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="loadFactor" /> is less than 0.1. /// /// -or- /// <paramref name="loadFactor" /> is greater than 1.0. /// </exception> [Obsolete("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")] public Hashtable(IDictionary d, float loadFactor, IHashCodeProvider hcp, IComparer comparer) : this((d != null) ? d.Count : 0, loadFactor, hcp, comparer) { if (d == null) { throw new ArgumentNullException("d", Environment.GetResourceString("ArgumentNull_Dictionary")); } IDictionaryEnumerator enumerator = d.GetEnumerator(); while (enumerator.MoveNext()) { this.Add(enumerator.Key, enumerator.Value); } } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.Hashtable" /> class by copying the elements from the specified dictionary to the new <see cref="T:System.Collections.Hashtable" /> object. The new <see cref="T:System.Collections.Hashtable" /> object has an initial capacity equal to the number of elements copied, and uses the specified load factor and <see cref="T:System.Collections.IEqualityComparer" /> object. /// </summary> /// <param name="d"> /// The <see cref="T:System.Collections.IDictionary" /> object to copy to a new <see cref="T:System.Collections.Hashtable" /> object. /// </param> /// <param name="loadFactor"> /// A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. The result is the maximum ratio of elements to buckets. /// </param> /// <param name="equalityComparer"> /// The <see cref="T:System.Collections.IEqualityComparer" /> object that defines the hash code provider and the comparer to use with the <see cref="T:System.Collections.Hashtable" />. /// /// -or- /// null to use the default hash code provider and the default comparer. The default hash code provider is each key's implementation of <see cref="M:System.Object.GetHashCode" /> and the default comparer is each key's implementation of <see cref="M:System.Object.Equals(System.Object)" />. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="d" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="loadFactor" /> is less than 0.1. /// /// -or- /// <paramref name="loadFactor" /> is greater than 1.0. /// </exception> public Hashtable(IDictionary d, float loadFactor, IEqualityComparer equalityComparer) : this((d != null) ? d.Count : 0, loadFactor, equalityComparer) { if (d == null) { throw new ArgumentNullException("d", Environment.GetResourceString("ArgumentNull_Dictionary")); } IDictionaryEnumerator enumerator = d.GetEnumerator(); while (enumerator.MoveNext()) { this.Add(enumerator.Key, enumerator.Value); } } /// <summary> /// Initializes a new, empty instance of the <see cref="T:System.Collections.Hashtable" /> class that is serializable using the specified <see cref="T:System.Runtime.Serialization.SerializationInfo" /> and <see cref="T:System.Runtime.Serialization.StreamingContext" /> objects. /// </summary> /// <param name="info"> /// A <see cref="T:System.Runtime.Serialization.SerializationInfo" /> object containing the information required to serialize the <see cref="T:System.Collections.Hashtable" /> object. /// </param> /// <param name="context"> /// A <see cref="T:System.Runtime.Serialization.StreamingContext" /> object containing the source and destination of the serialized stream associated with the <see cref="T:System.Collections.Hashtable" />. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="info" /> is null. /// </exception> protected Hashtable(SerializationInfo info, StreamingContext context) { this.m_siInfo = info; } private uint InitHash(object key, int hashsize, out uint seed, out uint incr) { uint num = (uint)(this.GetHash(key) & 2147483647); seed = num; incr = 1u + ((seed >> 5) + 1u) % (uint)(hashsize - 1); return num; } /// <summary> /// Adds an element with the specified key and value into the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <param name="key"> /// The key of the element to add. /// </param> /// <param name="value"> /// The value of the element to add. The value can be null. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.ArgumentException"> /// An element with the same key already exists in the <see cref="T:System.Collections.Hashtable" />. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.Hashtable" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.Hashtable" /> has a fixed size. /// </exception> /// <filterpriority>1</filterpriority> public virtual void Add(object key, object value) { this.Insert(key, value, true); } /// <summary> /// Removes all elements from the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.Hashtable" /> is read-only. /// </exception> /// <filterpriority>1</filterpriority> [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] public virtual void Clear() { if (this.count == 0) { return; } Thread.BeginCriticalRegion(); this.isWriterInProgress = true; for (int i = 0; i < this.buckets.Length; i++) { this.buckets[i].hash_coll = 0; this.buckets[i].key = null; this.buckets[i].val = null; } this.count = 0; this.occupancy = 0; this.UpdateVersion(); this.isWriterInProgress = false; Thread.EndCriticalRegion(); } /// <summary> /// Creates a shallow copy of the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <returns> /// A shallow copy of the <see cref="T:System.Collections.Hashtable" />. /// </returns> /// <filterpriority>1</filterpriority> public virtual object Clone() { Hashtable.bucket[] array = this.buckets; Hashtable hashtable = new Hashtable(this.count, this._keycomparer); hashtable.version = this.version; hashtable.loadFactor = this.loadFactor; hashtable.count = 0; int i = array.Length; while (i > 0) { i--; object key = array[i].key; if (key != null && key != array) { hashtable[key] = array[i].val; } } return hashtable; } /// <summary> /// Determines whether the <see cref="T:System.Collections.Hashtable" /> contains a specific key. /// </summary> /// <returns>true if the <see cref="T:System.Collections.Hashtable" /> contains an element with the specified key; otherwise, false. /// </returns> /// <param name="key"> /// The key to locate in the <see cref="T:System.Collections.Hashtable" />. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <filterpriority>1</filterpriority> public virtual bool Contains(object key) { return this.ContainsKey(key); } /// <summary> /// Determines whether the <see cref="T:System.Collections.Hashtable" /> contains a specific key. /// </summary> /// <returns>true if the <see cref="T:System.Collections.Hashtable" /> contains an element with the specified key; otherwise, false. /// </returns> /// <param name="key"> /// The key to locate in the <see cref="T:System.Collections.Hashtable" />. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <filterpriority>1</filterpriority> public virtual bool ContainsKey(object key) { if (key == null) { throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); } Hashtable.bucket[] array = this.buckets; uint num2; uint num3; uint num = this.InitHash(key, array.Length, out num2, out num3); int num4 = 0; int num5 = (int)(num2 % (uint)array.Length); while (true) { Hashtable.bucket bucket = array[num5]; if (bucket.key == null) { break; } if ((long)(bucket.hash_coll & 2147483647) == (long)((ulong)num) && this.KeyEquals(bucket.key, key)) { return true; } num5 = (int)(((long)num5 + (long)((ulong)num3)) % (long)((ulong)array.Length)); if (bucket.hash_coll >= 0 || ++num4 >= array.Length) { return false; } } return false; } /// <summary> /// Determines whether the <see cref="T:System.Collections.Hashtable" /> contains a specific value. /// </summary> /// <returns>true if the <see cref="T:System.Collections.Hashtable" /> contains an element with the specified <paramref name="value" />; otherwise, false. /// </returns> /// <param name="value"> /// The value to locate in the <see cref="T:System.Collections.Hashtable" />. The value can be null. /// </param> /// <filterpriority>1</filterpriority> public virtual bool ContainsValue(object value) { if (value == null) { int num = this.buckets.Length; while (--num >= 0) { if (this.buckets[num].key != null && this.buckets[num].key != this.buckets && this.buckets[num].val == null) { return true; } } } else { int num2 = this.buckets.Length; while (--num2 >= 0) { object val = this.buckets[num2].val; if (val != null && val.Equals(value)) { return true; } } } return false; } private void CopyKeys(Array array, int arrayIndex) { Hashtable.bucket[] array2 = this.buckets; int num = array2.Length; while (--num >= 0) { object key = array2[num].key; if (key != null && key != this.buckets) { array.SetValue(key, arrayIndex++); } } } private void CopyEntries(Array array, int arrayIndex) { Hashtable.bucket[] array2 = this.buckets; int num = array2.Length; while (--num >= 0) { object key = array2[num].key; if (key != null && key != this.buckets) { DictionaryEntry dictionaryEntry = new DictionaryEntry(key, array2[num].val); array.SetValue(dictionaryEntry, arrayIndex++); } } } /// <summary> /// Copies the <see cref="T:System.Collections.Hashtable" /> elements to a one-dimensional <see cref="T:System.Array" /> instance at the specified index. /// </summary> /// <param name="array"> /// The one-dimensional <see cref="T:System.Array" /> that is the destination of the <see cref="T:System.Collections.DictionaryEntry" /> objects copied from <see cref="T:System.Collections.Hashtable" />. The <see cref="T:System.Array" /> must have zero-based indexing. /// </param> /// <param name="arrayIndex"> /// The zero-based index in <paramref name="array" /> at which copying begins. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="array" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="arrayIndex" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="array" /> is multidimensional. /// /// -or- /// <paramref name="arrayIndex" /> is equal to or greater than the length of <paramref name="array" />. /// /// -or- /// /// The number of elements in the source <see cref="T:System.Collections.Hashtable" /> is greater than the available space from <paramref name="arrayIndex" /> to the end of the destination <paramref name="array" />. /// </exception> /// <exception cref="T:System.InvalidCastException"> /// The type of the source <see cref="T:System.Collections.Hashtable" /> cannot be cast automatically to the type of the destination <paramref name="array" />. /// </exception> /// <filterpriority>2</filterpriority> public virtual void CopyTo(Array array, int arrayIndex) { if (array == null) { throw new ArgumentNullException("array", Environment.GetResourceString("ArgumentNull_Array")); } if (array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } if (arrayIndex < 0) { throw new ArgumentOutOfRangeException("arrayIndex", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (array.Length - arrayIndex < this.count) { throw new ArgumentException(Environment.GetResourceString("Arg_ArrayPlusOffTooSmall")); } this.CopyEntries(array, arrayIndex); } internal virtual KeyValuePairs[] ToKeyValuePairsArray() { KeyValuePairs[] array = new KeyValuePairs[this.count]; int num = 0; Hashtable.bucket[] array2 = this.buckets; int num2 = array2.Length; while (--num2 >= 0) { object key = array2[num2].key; if (key != null && key != this.buckets) { array[num++] = new KeyValuePairs(key, array2[num2].val); } } return array; } private void CopyValues(Array array, int arrayIndex) { Hashtable.bucket[] array2 = this.buckets; int num = array2.Length; while (--num >= 0) { object key = array2[num].key; if (key != null && key != this.buckets) { array.SetValue(array2[num].val, arrayIndex++); } } } private void expand() { int prime = HashHelpers.GetPrime(this.buckets.Length * 2); this.rehash(prime); } private void rehash() { this.rehash(this.buckets.Length); } private void UpdateVersion() { this.version++; } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] private void rehash(int newsize) { this.occupancy = 0; Hashtable.bucket[] newBuckets = new Hashtable.bucket[newsize]; for (int i = 0; i < this.buckets.Length; i++) { Hashtable.bucket bucket = this.buckets[i]; if (bucket.key != null && bucket.key != this.buckets) { this.putEntry(newBuckets, bucket.key, bucket.val, bucket.hash_coll & 2147483647); } } Thread.BeginCriticalRegion(); this.isWriterInProgress = true; this.buckets = newBuckets; this.loadsize = (int)(this.loadFactor * (float)newsize); this.UpdateVersion(); this.isWriterInProgress = false; Thread.EndCriticalRegion(); } /// <summary> /// Returns an enumerator that iterates through a collection. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IEnumerator" /> that can be used to iterate through the collection. /// </returns> IEnumerator IEnumerable.GetEnumerator() { return new Hashtable.HashtableEnumerator(this, 3); } /// <summary> /// Returns an <see cref="T:System.Collections.IDictionaryEnumerator" /> that iterates through the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IDictionaryEnumerator" /> for the <see cref="T:System.Collections.Hashtable" />. /// </returns> /// <filterpriority>2</filterpriority> public virtual IDictionaryEnumerator GetEnumerator() { return new Hashtable.HashtableEnumerator(this, 3); } /// <summary> /// Returns the hash code for the specified key. /// </summary> /// <returns> /// The hash code for <paramref name="key" />. /// </returns> /// <param name="key"> /// The <see cref="T:System.Object" /> for which a hash code is to be returned. /// </param> /// <exception cref="T:System.NullReferenceException"> /// <paramref name="key" /> is null. /// </exception> protected virtual int GetHash(object key) { if (this._keycomparer != null) { return this._keycomparer.GetHashCode(key); } return key.GetHashCode(); } /// <summary> /// Compares a specific <see cref="T:System.Object" /> with a specific key in the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <returns>true if <paramref name="item" /> and <paramref name="key" /> are equal; otherwise, false. /// </returns> /// <param name="item"> /// The <see cref="T:System.Object" /> to compare with <paramref name="key" />. /// </param> /// <param name="key"> /// The key in the <see cref="T:System.Collections.Hashtable" /> to compare with <paramref name="item" />. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="item" /> is null. /// /// -or- /// <paramref name="key" /> is null. /// </exception> protected virtual bool KeyEquals(object item, object key) { if (object.ReferenceEquals(this.buckets, item)) { return false; } if (this._keycomparer != null) { return this._keycomparer.Equals(item, key); } return item != null && item.Equals(key); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] private void Insert(object key, object nvalue, bool add) { if (key == null) { throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); } if (this.count >= this.loadsize) { this.expand(); } else { if (this.occupancy > this.loadsize && this.count > 100) { this.rehash(); } } uint num2; uint num3; uint num = this.InitHash(key, this.buckets.Length, out num2, out num3); int num4 = 0; int num5 = -1; int num6 = (int)(num2 % (uint)this.buckets.Length); while (true) { if (num5 == -1 && this.buckets[num6].key == this.buckets && this.buckets[num6].hash_coll < 0) { num5 = num6; } if (this.buckets[num6].key == null || (this.buckets[num6].key == this.buckets && ((long)this.buckets[num6].hash_coll & (long)((ulong)-2147483648)) == 0L)) { break; } if ((long)(this.buckets[num6].hash_coll & 2147483647) == (long)((ulong)num) && this.KeyEquals(this.buckets[num6].key, key)) { goto Block_12; } if (num5 == -1 && this.buckets[num6].hash_coll >= 0) { Hashtable.bucket[] expr_242_cp_0 = this.buckets; int expr_242_cp_1 = num6; expr_242_cp_0[expr_242_cp_1].hash_coll = (expr_242_cp_0[expr_242_cp_1].hash_coll | -2147483648); this.occupancy++; } num6 = (int)(((long)num6 + (long)((ulong)num3)) % (long)((ulong)this.buckets.Length)); if (++num4 >= this.buckets.Length) { goto Block_16; } } if (num5 != -1) { num6 = num5; } Thread.BeginCriticalRegion(); this.isWriterInProgress = true; this.buckets[num6].val = nvalue; this.buckets[num6].key = key; Hashtable.bucket[] expr_142_cp_0 = this.buckets; int expr_142_cp_1 = num6; expr_142_cp_0[expr_142_cp_1].hash_coll = (expr_142_cp_0[expr_142_cp_1].hash_coll | (int)num); this.count++; this.UpdateVersion(); this.isWriterInProgress = false; Thread.EndCriticalRegion(); return; Block_12: if (add) { throw new ArgumentException(Environment.GetResourceString("Argument_AddingDuplicate__", new object[] { this.buckets[num6].key, key })); } Thread.BeginCriticalRegion(); this.isWriterInProgress = true; this.buckets[num6].val = nvalue; this.UpdateVersion(); this.isWriterInProgress = false; Thread.EndCriticalRegion(); return; Block_16: if (num5 != -1) { Thread.BeginCriticalRegion(); this.isWriterInProgress = true; this.buckets[num5].val = nvalue; this.buckets[num5].key = key; Hashtable.bucket[] expr_2CC_cp_0 = this.buckets; int expr_2CC_cp_1 = num5; expr_2CC_cp_0[expr_2CC_cp_1].hash_coll = (expr_2CC_cp_0[expr_2CC_cp_1].hash_coll | (int)num); this.count++; this.UpdateVersion(); this.isWriterInProgress = false; Thread.EndCriticalRegion(); return; } throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_HashInsertFailed")); } private void putEntry(Hashtable.bucket[] newBuckets, object key, object nvalue, int hashcode) { uint num = 1u + (((uint)hashcode >> 5) + 1u) % (uint)(newBuckets.Length - 1); int num2 = hashcode % newBuckets.Length; while (newBuckets[num2].key != null && newBuckets[num2].key != this.buckets) { if (newBuckets[num2].hash_coll >= 0) { int expr_7F_cp_1 = num2; newBuckets[expr_7F_cp_1].hash_coll = (newBuckets[expr_7F_cp_1].hash_coll | -2147483648); this.occupancy++; } num2 = (int)(((long)num2 + (long)((ulong)num)) % (long)((ulong)newBuckets.Length)); } newBuckets[num2].val = nvalue; newBuckets[num2].key = key; int expr_5A_cp_1 = num2; newBuckets[expr_5A_cp_1].hash_coll = (newBuckets[expr_5A_cp_1].hash_coll | hashcode); } /// <summary> /// Removes the element with the specified key from the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <param name="key"> /// The key of the element to remove. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.Hashtable" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.Hashtable" /> has a fixed size. /// </exception> /// <filterpriority>1</filterpriority> [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public virtual void Remove(object key) { if (key == null) { throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); } uint num2; uint num3; uint num = this.InitHash(key, this.buckets.Length, out num2, out num3); int num4 = 0; int num5 = (int)(num2 % (uint)this.buckets.Length); while (true) { Hashtable.bucket bucket = this.buckets[num5]; if ((long)(bucket.hash_coll & 2147483647) == (long)((ulong)num) && this.KeyEquals(bucket.key, key)) { break; } num5 = (int)(((long)num5 + (long)((ulong)num3)) % (long)((ulong)this.buckets.Length)); if (bucket.hash_coll >= 0 || ++num4 >= this.buckets.Length) { return; } } Thread.BeginCriticalRegion(); this.isWriterInProgress = true; Hashtable.bucket[] expr_91_cp_0 = this.buckets; int expr_91_cp_1 = num5; expr_91_cp_0[expr_91_cp_1].hash_coll = (expr_91_cp_0[expr_91_cp_1].hash_coll & -2147483648); if (this.buckets[num5].hash_coll != 0) { this.buckets[num5].key = this.buckets; } else { this.buckets[num5].key = null; } this.buckets[num5].val = null; this.count--; this.UpdateVersion(); this.isWriterInProgress = false; Thread.EndCriticalRegion(); } /// <summary> /// Returns a synchronized (thread safe) wrapper for the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <returns> /// A synchronized (thread safe) wrapper for the <see cref="T:System.Collections.Hashtable" />. /// </returns> /// <param name="table"> /// The <see cref="T:System.Collections.Hashtable" /> to synchronize. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="table" /> is null. /// </exception> /// <filterpriority>1</filterpriority> [HostProtection(SecurityAction.LinkDemand, Synchronization = true)] public static Hashtable Synchronized(Hashtable table) { if (table == null) { throw new ArgumentNullException("table"); } return new Hashtable.SyncHashtable(table); } /// <summary> /// Implements the <see cref="T:System.Runtime.Serialization.ISerializable" /> interface and returns the data needed to serialize the <see cref="T:System.Collections.Hashtable" />. /// </summary> /// <param name="info"> /// A <see cref="T:System.Runtime.Serialization.SerializationInfo" /> object containing the information required to serialize the <see cref="T:System.Collections.Hashtable" />. /// </param> /// <param name="context"> /// A <see cref="T:System.Runtime.Serialization.StreamingContext" /> object containing the source and destination of the serialized stream associated with the <see cref="T:System.Collections.Hashtable" />. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="info" /> is null. /// </exception> /// <filterpriority>2</filterpriority> public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException("info"); } info.AddValue("LoadFactor", this.loadFactor); info.AddValue("Version", this.version); if (this._keycomparer == null) { info.AddValue("Comparer", null, typeof(IComparer)); info.AddValue("HashCodeProvider", null, typeof(IHashCodeProvider)); } else { if (this._keycomparer is CompatibleComparer) { CompatibleComparer compatibleComparer = this._keycomparer as CompatibleComparer; info.AddValue("Comparer", compatibleComparer.Comparer, typeof(IComparer)); info.AddValue("HashCodeProvider", compatibleComparer.HashCodeProvider, typeof(IHashCodeProvider)); } else { info.AddValue("KeyComparer", this._keycomparer, typeof(IEqualityComparer)); } } info.AddValue("HashSize", this.buckets.Length); object[] array = new object[this.count]; object[] array2 = new object[this.count]; this.CopyKeys(array, 0); this.CopyValues(array2, 0); info.AddValue("Keys", array, typeof(object[])); info.AddValue("Values", array2, typeof(object[])); } /// <summary> /// Implements the <see cref="T:System.Runtime.Serialization.ISerializable" /> interface and raises the deserialization event when the deserialization is complete. /// </summary> /// <param name="sender"> /// The source of the deserialization event. /// </param> /// <exception cref="T:System.Runtime.Serialization.SerializationException"> /// The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> object associated with the current <see cref="T:System.Collections.Hashtable" /> is invalid. /// </exception> /// <filterpriority>2</filterpriority> public virtual void OnDeserialization(object sender) { if (this.buckets != null) { return; } if (this.m_siInfo == null) { throw new SerializationException(Environment.GetResourceString("Serialization_InvalidOnDeser")); } int num = 0; IComparer comparer = null; IHashCodeProvider hashCodeProvider = null; object[] array = null; object[] array2 = null; SerializationInfoEnumerator enumerator = this.m_siInfo.GetEnumerator(); while (enumerator.MoveNext()) { string name; switch (name = enumerator.Name) { case "LoadFactor": this.loadFactor = this.m_siInfo.GetSingle("LoadFactor"); break; case "HashSize": num = this.m_siInfo.GetInt32("HashSize"); break; case "KeyComparer": this._keycomparer = (IEqualityComparer)this.m_siInfo.GetValue("KeyComparer", typeof(IEqualityComparer)); break; case "Comparer": comparer = (IComparer)this.m_siInfo.GetValue("Comparer", typeof(IComparer)); break; case "HashCodeProvider": hashCodeProvider = (IHashCodeProvider)this.m_siInfo.GetValue("HashCodeProvider", typeof(IHashCodeProvider)); break; case "Keys": array = (object[])this.m_siInfo.GetValue("Keys", typeof(object[])); break; case "Values": array2 = (object[])this.m_siInfo.GetValue("Values", typeof(object[])); break; } } this.loadsize = (int)(this.loadFactor * (float)num); if (this._keycomparer == null && (comparer != null || hashCodeProvider != null)) { this._keycomparer = new CompatibleComparer(comparer, hashCodeProvider); } this.buckets = new Hashtable.bucket[num]; if (array == null) { throw new SerializationException(Environment.GetResourceString("Serialization_MissingKeys")); } if (array2 == null) { throw new SerializationException(Environment.GetResourceString("Serialization_MissingValues")); } if (array.Length != array2.Length) { throw new SerializationException(Environment.GetResourceString("Serialization_KeyValueDifferentSizes")); } for (int i = 0; i < array.Length; i++) { if (array[i] == null) { throw new SerializationException(Environment.GetResourceString("Serialization_NullKey")); } this.Insert(array[i], array2[i], true); } this.version = this.m_siInfo.GetInt32("Version"); this.m_siInfo = null; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Diagnostics; using System.Globalization; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Threading; namespace System.Collections { /// <summary> /// Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index. /// </summary> /// <filterpriority>1</filterpriority> [DebuggerDisplay("Count = {Count}"), DebuggerTypeProxy(typeof(SortedList.SortedListDebugView)), ComVisible(true)] [Serializable] public class SortedList : IDictionary, ICollection, IEnumerable, ICloneable { [Serializable] private class SyncSortedList : SortedList { private SortedList _list; private object _root; public override int Count { get { object root; Monitor.Enter(root = this._root); int count; try { count = this._list.Count; } finally { Monitor.Exit(root); } return count; } } public override object SyncRoot { get { return this._root; } } public override bool IsReadOnly { get { return this._list.IsReadOnly; } } public override bool IsFixedSize { get { return this._list.IsFixedSize; } } public override bool IsSynchronized { get { return true; } } public override object this[object key] { get { object root; Monitor.Enter(root = this._root); object result; try { result = this._list[key]; } finally { Monitor.Exit(root); } return result; } set { object root; Monitor.Enter(root = this._root); try { this._list[key] = value; } finally { Monitor.Exit(root); } } } public override int Capacity { get { object root; Monitor.Enter(root = this._root); int capacity; try { capacity = this._list.Capacity; } finally { Monitor.Exit(root); } return capacity; } } internal SyncSortedList(SortedList list) { this._list = list; this._root = list.SyncRoot; } public override void Add(object key, object value) { object root; Monitor.Enter(root = this._root); try { this._list.Add(key, value); } finally { Monitor.Exit(root); } } public override void Clear() { object root; Monitor.Enter(root = this._root); try { this._list.Clear(); } finally { Monitor.Exit(root); } } public override object Clone() { object root; Monitor.Enter(root = this._root); object result; try { result = this._list.Clone(); } finally { Monitor.Exit(root); } return result; } public override bool Contains(object key) { object root; Monitor.Enter(root = this._root); bool result; try { result = this._list.Contains(key); } finally { Monitor.Exit(root); } return result; } public override bool ContainsKey(object key) { object root; Monitor.Enter(root = this._root); bool result; try { result = this._list.ContainsKey(key); } finally { Monitor.Exit(root); } return result; } public override bool ContainsValue(object key) { object root; Monitor.Enter(root = this._root); bool result; try { result = this._list.ContainsValue(key); } finally { Monitor.Exit(root); } return result; } public override void CopyTo(Array array, int index) { object root; Monitor.Enter(root = this._root); try { this._list.CopyTo(array, index); } finally { Monitor.Exit(root); } } public override object GetByIndex(int index) { object root; Monitor.Enter(root = this._root); object byIndex; try { byIndex = this._list.GetByIndex(index); } finally { Monitor.Exit(root); } return byIndex; } public override IDictionaryEnumerator GetEnumerator() { object root; Monitor.Enter(root = this._root); IDictionaryEnumerator enumerator; try { enumerator = this._list.GetEnumerator(); } finally { Monitor.Exit(root); } return enumerator; } public override object GetKey(int index) { object root; Monitor.Enter(root = this._root); object key; try { key = this._list.GetKey(index); } finally { Monitor.Exit(root); } return key; } public override IList GetKeyList() { object root; Monitor.Enter(root = this._root); IList keyList; try { keyList = this._list.GetKeyList(); } finally { Monitor.Exit(root); } return keyList; } public override IList GetValueList() { object root; Monitor.Enter(root = this._root); IList valueList; try { valueList = this._list.GetValueList(); } finally { Monitor.Exit(root); } return valueList; } public override int IndexOfKey(object key) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.IndexOfKey(key); } finally { Monitor.Exit(root); } return result; } public override int IndexOfValue(object value) { object root; Monitor.Enter(root = this._root); int result; try { result = this._list.IndexOfValue(value); } finally { Monitor.Exit(root); } return result; } public override void RemoveAt(int index) { object root; Monitor.Enter(root = this._root); try { this._list.RemoveAt(index); } finally { Monitor.Exit(root); } } public override void Remove(object key) { object root; Monitor.Enter(root = this._root); try { this._list.Remove(key); } finally { Monitor.Exit(root); } } public override void SetByIndex(int index, object value) { object root; Monitor.Enter(root = this._root); try { this._list.SetByIndex(index, value); } finally { Monitor.Exit(root); } } internal override KeyValuePairs[] ToKeyValuePairsArray() { return this._list.ToKeyValuePairsArray(); } public override void TrimToSize() { object root; Monitor.Enter(root = this._root); try { this._list.TrimToSize(); } finally { Monitor.Exit(root); } } } [Serializable] private class SortedListEnumerator : IDictionaryEnumerator, IEnumerator, ICloneable { internal const int Keys = 1; internal const int Values = 2; internal const int DictEntry = 3; private SortedList sortedList; private object key; private object value; private int index; private int startIndex; private int endIndex; private int version; private bool current; private int getObjectRetType; public virtual object Key { get { if (this.version != this.sortedList.version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } if (!this.current) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen")); } return this.key; } } public virtual DictionaryEntry Entry { get { if (this.version != this.sortedList.version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } if (!this.current) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen")); } return new DictionaryEntry(this.key, this.value); } } public virtual object Current { get { if (!this.current) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen")); } if (this.getObjectRetType == 1) { return this.key; } if (this.getObjectRetType == 2) { return this.value; } return new DictionaryEntry(this.key, this.value); } } public virtual object Value { get { if (this.version != this.sortedList.version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } if (!this.current) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen")); } return this.value; } } internal SortedListEnumerator(SortedList sortedList, int index, int count, int getObjRetType) { this.sortedList = sortedList; this.index = index; this.startIndex = index; this.endIndex = index + count; this.version = sortedList.version; this.getObjectRetType = getObjRetType; this.current = false; } public object Clone() { return base.MemberwiseClone(); } public virtual bool MoveNext() { if (this.version != this.sortedList.version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } if (this.index < this.endIndex) { this.key = this.sortedList.keys[this.index]; this.value = this.sortedList.values[this.index]; this.index++; this.current = true; return true; } this.key = null; this.value = null; this.current = false; return false; } public virtual void Reset() { if (this.version != this.sortedList.version) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion")); } this.index = this.startIndex; this.current = false; this.key = null; this.value = null; } } [Serializable] private class KeyList : IList, ICollection, IEnumerable { private SortedList sortedList; public virtual int Count { get { return this.sortedList._size; } } public virtual bool IsReadOnly { get { return true; } } public virtual bool IsFixedSize { get { return true; } } public virtual bool IsSynchronized { get { return this.sortedList.IsSynchronized; } } public virtual object SyncRoot { get { return this.sortedList.SyncRoot; } } public virtual object this[int index] { get { return this.sortedList.GetKey(index); } set { throw new NotSupportedException(Environment.GetResourceString("NotSupported_KeyCollectionSet")); } } internal KeyList(SortedList sortedList) { this.sortedList = sortedList; } public virtual int Add(object key) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_SortedListNestedWrite")); } public virtual void Clear() { throw new NotSupportedException(Environment.GetResourceString("NotSupported_SortedListNestedWrite")); } public virtual bool Contains(object key) { return this.sortedList.Contains(key); } public virtual void CopyTo(Array array, int arrayIndex) { if (array != null && array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } Array.Copy(this.sortedList.keys, 0, array, arrayIndex, this.sortedList.Count); } public virtual void Insert(int index, object value) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_SortedListNestedWrite")); } public virtual IEnumerator GetEnumerator() { return new SortedList.SortedListEnumerator(this.sortedList, 0, this.sortedList.Count, 1); } public virtual int IndexOf(object key) { if (key == null) { throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); } int num = Array.BinarySearch(this.sortedList.keys, 0, this.sortedList.Count, key, this.sortedList.comparer); if (num >= 0) { return num; } return -1; } public virtual void Remove(object key) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_SortedListNestedWrite")); } public virtual void RemoveAt(int index) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_SortedListNestedWrite")); } } [Serializable] private class ValueList : IList, ICollection, IEnumerable { private SortedList sortedList; public virtual int Count { get { return this.sortedList._size; } } public virtual bool IsReadOnly { get { return true; } } public virtual bool IsFixedSize { get { return true; } } public virtual bool IsSynchronized { get { return this.sortedList.IsSynchronized; } } public virtual object SyncRoot { get { return this.sortedList.SyncRoot; } } public virtual object this[int index] { get { return this.sortedList.GetByIndex(index); } set { throw new NotSupportedException(Environment.GetResourceString("NotSupported_SortedListNestedWrite")); } } internal ValueList(SortedList sortedList) { this.sortedList = sortedList; } public virtual int Add(object key) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_SortedListNestedWrite")); } public virtual void Clear() { throw new NotSupportedException(Environment.GetResourceString("NotSupported_SortedListNestedWrite")); } public virtual bool Contains(object value) { return this.sortedList.ContainsValue(value); } public virtual void CopyTo(Array array, int arrayIndex) { if (array != null && array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } Array.Copy(this.sortedList.values, 0, array, arrayIndex, this.sortedList.Count); } public virtual void Insert(int index, object value) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_SortedListNestedWrite")); } public virtual IEnumerator GetEnumerator() { return new SortedList.SortedListEnumerator(this.sortedList, 0, this.sortedList.Count, 2); } public virtual int IndexOf(object value) { return Array.IndexOf<object>(this.sortedList.values, value, 0, this.sortedList.Count); } public virtual void Remove(object value) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_SortedListNestedWrite")); } public virtual void RemoveAt(int index) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_SortedListNestedWrite")); } } internal class SortedListDebugView { private SortedList sortedList; [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public KeyValuePairs[] Items { get { return this.sortedList.ToKeyValuePairsArray(); } } public SortedListDebugView(SortedList sortedList) { if (sortedList == null) { throw new ArgumentNullException("sortedList"); } this.sortedList = sortedList; } } private const int _defaultCapacity = 16; private object[] keys; private object[] values; private int _size; private int version; private IComparer comparer; private SortedList.KeyList keyList; private SortedList.ValueList valueList; [NonSerialized] private object _syncRoot; private static object[] emptyArray = new object[0]; /// <summary> /// Gets or sets the capacity of a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// The number of elements that the <see cref="T:System.Collections.SortedList" /> object can contain. /// </returns> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// The value assigned is less than the current number of elements in the <see cref="T:System.Collections.SortedList" /> object. /// </exception> /// <exception cref="T:System.OutOfMemoryException"> /// There is not enough memory available on the system. /// </exception> /// <filterpriority>2</filterpriority> public virtual int Capacity { get { return this.keys.Length; } set { if (value != this.keys.Length) { if (value < this._size) { throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_SmallCapacity")); } if (value > 0) { object[] destinationArray = new object[value]; object[] destinationArray2 = new object[value]; if (this._size > 0) { Array.Copy(this.keys, 0, destinationArray, 0, this._size); Array.Copy(this.values, 0, destinationArray2, 0, this._size); } this.keys = destinationArray; this.values = destinationArray2; return; } this.keys = SortedList.emptyArray; this.values = SortedList.emptyArray; } } } /// <summary> /// Gets the number of elements contained in a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// The number of elements contained in the <see cref="T:System.Collections.SortedList" /> object. /// </returns> /// <filterpriority>1</filterpriority> public virtual int Count { get { return this._size; } } /// <summary> /// Gets the keys in a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// An <see cref="T:System.Collections.ICollection" /> object containing the keys in the <see cref="T:System.Collections.SortedList" /> object. /// </returns> /// <filterpriority>1</filterpriority> public virtual ICollection Keys { get { return this.GetKeyList(); } } /// <summary> /// Gets the values in a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// An <see cref="T:System.Collections.ICollection" /> object containing the values in the <see cref="T:System.Collections.SortedList" /> object. /// </returns> /// <filterpriority>1</filterpriority> public virtual ICollection Values { get { return this.GetValueList(); } } /// <summary> /// Gets a value indicating whether a <see cref="T:System.Collections.SortedList" /> object is read-only. /// </summary> /// <returns>true if the <see cref="T:System.Collections.SortedList" /> object is read-only; otherwise, false. The default is false. /// </returns> /// <filterpriority>2</filterpriority> public virtual bool IsReadOnly { get { return false; } } /// <summary> /// Gets a value indicating whether a <see cref="T:System.Collections.SortedList" /> object has a fixed size. /// </summary> /// <returns>true if the <see cref="T:System.Collections.SortedList" /> object has a fixed size; otherwise, false. The default is false. /// </returns> /// <filterpriority>2</filterpriority> public virtual bool IsFixedSize { get { return false; } } /// <summary> /// Gets a value indicating whether access to a <see cref="T:System.Collections.SortedList" /> object is synchronized (thread safe). /// </summary> /// <returns>true if access to the <see cref="T:System.Collections.SortedList" /> object is synchronized (thread safe); otherwise, false. The default is false. /// </returns> /// <filterpriority>2</filterpriority> public virtual bool IsSynchronized { get { return false; } } /// <summary> /// Gets an object that can be used to synchronize access to a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// An object that can be used to synchronize access to the <see cref="T:System.Collections.SortedList" /> object. /// </returns> /// <filterpriority>2</filterpriority> public virtual object SyncRoot { get { if (this._syncRoot == null) { Interlocked.CompareExchange(ref this._syncRoot, new object(), null); } return this._syncRoot; } } /// <summary> /// Gets and sets the value associated with a specific key in a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// The value associated with the <paramref name="key" /> parameter in the <see cref="T:System.Collections.SortedList" /> object, if <paramref name="key" /> is found; otherwise, null. /// </returns> /// <param name="key"> /// The key associated with the value to get or set. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The property is set and the <see cref="T:System.Collections.SortedList" /> object is read-only. /// /// -or- /// /// The property is set, <paramref name="key" /> does not exist in the collection, and the <see cref="T:System.Collections.SortedList" /> has a fixed size. /// </exception> /// <exception cref="T:System.OutOfMemoryException"> /// There is not enough available memory to add the element to the <see cref="T:System.Collections.SortedList" />. /// </exception> /// <exception cref="T:System.InvalidOperationException"> /// The comparer throws an exception. /// </exception> /// <filterpriority>1</filterpriority> public virtual object this[object key] { get { int num = this.IndexOfKey(key); if (num >= 0) { return this.values[num]; } return null; } set { if (key == null) { throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); } int num = Array.BinarySearch(this.keys, 0, this._size, key, this.comparer); if (num >= 0) { this.values[num] = value; this.version++; return; } this.Insert(~num, key, value); } } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.SortedList" /> class that is empty, has the default initial capacity, and is sorted according to the <see cref="T:System.IComparable" /> interface implemented by each key added to the <see cref="T:System.Collections.SortedList" /> object. /// </summary> public SortedList() { this.keys = SortedList.emptyArray; this.values = SortedList.emptyArray; this._size = 0; this.comparer = new Comparer(CultureInfo.CurrentCulture); } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.SortedList" /> class that is empty, has the specified initial capacity, and is sorted according to the <see cref="T:System.IComparable" /> interface implemented by each key added to the <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <param name="initialCapacity"> /// The initial number of elements that the <see cref="T:System.Collections.SortedList" /> object can contain. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="initialCapacity" /> is less than zero. /// </exception> /// <exception cref="T:System.OutOfMemoryException"> /// There is not enough available memory to create a <see cref="T:System.Collections.SortedList" /> object with the specified <paramref name="initialCapacity" />. /// </exception> public SortedList(int initialCapacity) { if (initialCapacity < 0) { throw new ArgumentOutOfRangeException("initialCapacity", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } this.keys = new object[initialCapacity]; this.values = new object[initialCapacity]; this.comparer = new Comparer(CultureInfo.CurrentCulture); } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.SortedList" /> class that is empty, has the default initial capacity, and is sorted according to the specified <see cref="T:System.Collections.IComparer" /> interface. /// </summary> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> implementation to use when comparing keys. /// /// -or- /// null to use the <see cref="T:System.IComparable" /> implementation of each key. /// </param> public SortedList(IComparer comparer) : this() { if (comparer != null) { this.comparer = comparer; } } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.SortedList" /> class that is empty, has the specified initial capacity, and is sorted according to the specified <see cref="T:System.Collections.IComparer" /> interface. /// </summary> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> implementation to use when comparing keys. /// /// -or- /// null to use the <see cref="T:System.IComparable" /> implementation of each key. /// </param> /// <param name="capacity"> /// The initial number of elements that the <see cref="T:System.Collections.SortedList" /> object can contain. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="capacity" /> is less than zero. /// </exception> /// <exception cref="T:System.OutOfMemoryException"> /// There is not enough available memory to create a <see cref="T:System.Collections.SortedList" /> object with the specified <paramref name="capacity" />. /// </exception> public SortedList(IComparer comparer, int capacity) : this(comparer) { this.Capacity = capacity; } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.SortedList" /> class that contains elements copied from the specified dictionary, has the same initial capacity as the number of elements copied, and is sorted according to the <see cref="T:System.IComparable" /> interface implemented by each key. /// </summary> /// <param name="d"> /// The <see cref="T:System.Collections.IDictionary" /> implementation to copy to a new <see cref="T:System.Collections.SortedList" /> object. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="d" /> is null. /// </exception> /// <exception cref="T:System.InvalidCastException"> /// One or more elements in <paramref name="d" /> do not implement the <see cref="T:System.IComparable" /> interface. /// </exception> public SortedList(IDictionary d) : this(d, null) { } /// <summary> /// Initializes a new instance of the <see cref="T:System.Collections.SortedList" /> class that contains elements copied from the specified dictionary, has the same initial capacity as the number of elements copied, and is sorted according to the specified <see cref="T:System.Collections.IComparer" /> interface. /// </summary> /// <param name="d"> /// The <see cref="T:System.Collections.IDictionary" /> implementation to copy to a new <see cref="T:System.Collections.SortedList" /> object. /// </param> /// <param name="comparer"> /// The <see cref="T:System.Collections.IComparer" /> implementation to use when comparing keys. /// /// -or- /// null to use the <see cref="T:System.IComparable" /> implementation of each key. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="d" /> is null. /// </exception> /// <exception cref="T:System.InvalidCastException"> /// <paramref name="comparer" /> is null, and one or more elements in <paramref name="d" /> do not implement the <see cref="T:System.IComparable" /> interface. /// </exception> public SortedList(IDictionary d, IComparer comparer) : this(comparer, (d != null) ? d.Count : 0) { if (d == null) { throw new ArgumentNullException("d", Environment.GetResourceString("ArgumentNull_Dictionary")); } d.Keys.CopyTo(this.keys, 0); d.Values.CopyTo(this.values, 0); Array.Sort(this.keys, this.values, comparer); this._size = d.Count; } /// <summary> /// Adds an element with the specified key and value to a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <param name="key"> /// The key of the element to add. /// </param> /// <param name="value"> /// The value of the element to add. The value can be null. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.ArgumentException"> /// An element with the specified <paramref name="key" /> already exists in the <see cref="T:System.Collections.SortedList" /> object. /// /// -or- /// /// The <see cref="T:System.Collections.SortedList" /> is set to use the <see cref="T:System.IComparable" /> interface, and <paramref name="key" /> does not implement the <see cref="T:System.IComparable" /> interface. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.SortedList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.SortedList" /> has a fixed size. /// </exception> /// <exception cref="T:System.OutOfMemoryException"> /// There is not enough available memory to add the element to the <see cref="T:System.Collections.SortedList" />. /// </exception> /// <exception cref="T:System.InvalidOperationException"> /// The comparer throws an exception. /// </exception> /// <filterpriority>2</filterpriority> public virtual void Add(object key, object value) { if (key == null) { throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); } int num = Array.BinarySearch(this.keys, 0, this._size, key, this.comparer); if (num >= 0) { throw new ArgumentException(Environment.GetResourceString("Argument_AddingDuplicate__", new object[] { this.GetKey(num), key })); } this.Insert(~num, key, value); } /// <summary> /// Removes all elements from a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.SortedList" /> object is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.SortedList" /> has a fixed size. /// </exception> /// <filterpriority>1</filterpriority> public virtual void Clear() { this.version++; Array.Clear(this.keys, 0, this._size); Array.Clear(this.values, 0, this._size); this._size = 0; } /// <summary> /// Creates a shallow copy of a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// A shallow copy of the <see cref="T:System.Collections.SortedList" /> object. /// </returns> /// <filterpriority>2</filterpriority> /// <PermissionSet> /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" /> /// </PermissionSet> public virtual object Clone() { SortedList sortedList = new SortedList(this._size); Array.Copy(this.keys, 0, sortedList.keys, 0, this._size); Array.Copy(this.values, 0, sortedList.values, 0, this._size); sortedList._size = this._size; sortedList.version = this.version; sortedList.comparer = this.comparer; return sortedList; } /// <summary> /// Determines whether a <see cref="T:System.Collections.SortedList" /> object contains a specific key. /// </summary> /// <returns>true if the <see cref="T:System.Collections.SortedList" /> object contains an element with the specified <paramref name="key" />; otherwise, false. /// </returns> /// <param name="key"> /// The key to locate in the <see cref="T:System.Collections.SortedList" /> object. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.InvalidOperationException"> /// The comparer throws an exception. /// </exception> /// <filterpriority>1</filterpriority> public virtual bool Contains(object key) { return this.IndexOfKey(key) >= 0; } /// <summary> /// Determines whether a <see cref="T:System.Collections.SortedList" /> object contains a specific key. /// </summary> /// <returns>true if the <see cref="T:System.Collections.SortedList" /> object contains an element with the specified <paramref name="key" />; otherwise, false. /// </returns> /// <param name="key"> /// The key to locate in the <see cref="T:System.Collections.SortedList" /> object. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.InvalidOperationException"> /// The comparer throws an exception. /// </exception> /// <filterpriority>1</filterpriority> public virtual bool ContainsKey(object key) { return this.IndexOfKey(key) >= 0; } /// <summary> /// Determines whether a <see cref="T:System.Collections.SortedList" /> object contains a specific value. /// </summary> /// <returns>true if the <see cref="T:System.Collections.SortedList" /> object contains an element with the specified <paramref name="value" />; otherwise, false. /// </returns> /// <param name="value"> /// The value to locate in the <see cref="T:System.Collections.SortedList" /> object. The value can be null. /// </param> /// <filterpriority>2</filterpriority> public virtual bool ContainsValue(object value) { return this.IndexOfValue(value) >= 0; } /// <summary> /// Copies <see cref="T:System.Collections.SortedList" /> elements to a one-dimensional <see cref="T:System.Array" /> object, starting at the specified index in the array. /// </summary> /// <param name="array"> /// The one-dimensional <see cref="T:System.Array" /> object that is the destination of the <see cref="T:System.Collections.DictionaryEntry" /> objects copied from <see cref="T:System.Collections.SortedList" />. The <see cref="T:System.Array" /> must have zero-based indexing. /// </param> /// <param name="arrayIndex"> /// The zero-based index in <paramref name="array" /> at which copying begins. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="array" /> is null. /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="arrayIndex" /> is less than zero. /// </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="array" /> is multidimensional. /// /// -or- /// <paramref name="arrayIndex" /> is equal to or greater than the length of <paramref name="array" />. /// /// -or- /// /// The number of elements in the source <see cref="T:System.Collections.SortedList" /> object is greater than the available space from <paramref name="arrayIndex" /> to the end of the destination <paramref name="array" />. /// </exception> /// <exception cref="T:System.InvalidCastException"> /// The type of the source <see cref="T:System.Collections.SortedList" /> cannot be cast automatically to the type of the destination <paramref name="array" />. /// </exception> /// <filterpriority>2</filterpriority> public virtual void CopyTo(Array array, int arrayIndex) { if (array == null) { throw new ArgumentNullException("array", Environment.GetResourceString("ArgumentNull_Array")); } if (array.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported")); } if (arrayIndex < 0) { throw new ArgumentOutOfRangeException("arrayIndex", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (array.Length - arrayIndex < this.Count) { throw new ArgumentException(Environment.GetResourceString("Arg_ArrayPlusOffTooSmall")); } for (int i = 0; i < this.Count; i++) { DictionaryEntry dictionaryEntry = new DictionaryEntry(this.keys[i], this.values[i]); array.SetValue(dictionaryEntry, i + arrayIndex); } } internal virtual KeyValuePairs[] ToKeyValuePairsArray() { KeyValuePairs[] array = new KeyValuePairs[this.Count]; for (int i = 0; i < this.Count; i++) { array[i] = new KeyValuePairs(this.keys[i], this.values[i]); } return array; } private void EnsureCapacity(int min) { int num = (this.keys.Length == 0) ? 16 : (this.keys.Length * 2); if (num < min) { num = min; } this.Capacity = num; } /// <summary> /// Gets the value at the specified index of a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// The value at the specified index of the <see cref="T:System.Collections.SortedList" /> object. /// </returns> /// <param name="index"> /// The zero-based index of the value to get. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is outside the range of valid indexes for the <see cref="T:System.Collections.SortedList" /> object. /// </exception> /// <filterpriority>2</filterpriority> public virtual object GetByIndex(int index) { if (index < 0 || index >= this._size) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } return this.values[index]; } /// <summary> /// Returns an <see cref="T:System.Collections.IEnumerator" /> that iterates through the <see cref="T:System.Collections.SortedList" />. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IEnumerator" /> for the <see cref="T:System.Collections.SortedList" />. /// </returns> IEnumerator IEnumerable.GetEnumerator() { return new SortedList.SortedListEnumerator(this, 0, this._size, 3); } /// <summary> /// Returns an <see cref="T:System.Collections.IDictionaryEnumerator" /> object that iterates through a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IDictionaryEnumerator" /> object for the <see cref="T:System.Collections.SortedList" /> object. /// </returns> /// <filterpriority>2</filterpriority> public virtual IDictionaryEnumerator GetEnumerator() { return new SortedList.SortedListEnumerator(this, 0, this._size, 3); } /// <summary> /// Gets the key at the specified index of a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// The key at the specified index of the <see cref="T:System.Collections.SortedList" /> object. /// </returns> /// <param name="index"> /// The zero-based index of the key to get. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is outside the range of valid indexes for the <see cref="T:System.Collections.SortedList" /> object. /// </exception> /// <filterpriority>2</filterpriority> public virtual object GetKey(int index) { if (index < 0 || index >= this._size) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } return this.keys[index]; } /// <summary> /// Gets the keys in a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IList" /> object containing the keys in the <see cref="T:System.Collections.SortedList" /> object. /// </returns> /// <filterpriority>2</filterpriority> public virtual IList GetKeyList() { if (this.keyList == null) { this.keyList = new SortedList.KeyList(this); } return this.keyList; } /// <summary> /// Gets the values in a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// An <see cref="T:System.Collections.IList" /> object containing the values in the <see cref="T:System.Collections.SortedList" /> object. /// </returns> /// <filterpriority>2</filterpriority> public virtual IList GetValueList() { if (this.valueList == null) { this.valueList = new SortedList.ValueList(this); } return this.valueList; } /// <summary> /// Returns the zero-based index of the specified key in a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// The zero-based index of the <paramref name="key" /> parameter, if <paramref name="key" /> is found in the <see cref="T:System.Collections.SortedList" /> object; otherwise, -1. /// </returns> /// <param name="key"> /// The key to locate in the <see cref="T:System.Collections.SortedList" /> object. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.InvalidOperationException"> /// The comparer throws an exception. /// </exception> /// <filterpriority>1</filterpriority> public virtual int IndexOfKey(object key) { if (key == null) { throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); } int num = Array.BinarySearch(this.keys, 0, this._size, key, this.comparer); if (num < 0) { return -1; } return num; } /// <summary> /// Returns the zero-based index of the first occurrence of the specified value in a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// The zero-based index of the first occurrence of the <paramref name="value" /> parameter, if <paramref name="value" /> is found in the <see cref="T:System.Collections.SortedList" /> object; otherwise, -1. /// </returns> /// <param name="value"> /// The value to locate in the <see cref="T:System.Collections.SortedList" /> object. The value can be null. /// </param> /// <filterpriority>1</filterpriority> public virtual int IndexOfValue(object value) { return Array.IndexOf<object>(this.values, value, 0, this._size); } private void Insert(int index, object key, object value) { if (this._size == this.keys.Length) { this.EnsureCapacity(this._size + 1); } if (index < this._size) { Array.Copy(this.keys, index, this.keys, index + 1, this._size - index); Array.Copy(this.values, index, this.values, index + 1, this._size - index); } this.keys[index] = key; this.values[index] = value; this._size++; this.version++; } /// <summary> /// Removes the element at the specified index of a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <param name="index"> /// The zero-based index of the element to remove. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is outside the range of valid indexes for the <see cref="T:System.Collections.SortedList" /> object. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.SortedList" /> is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.SortedList" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> public virtual void RemoveAt(int index) { if (index < 0 || index >= this._size) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } this._size--; if (index < this._size) { Array.Copy(this.keys, index + 1, this.keys, index, this._size - index); Array.Copy(this.values, index + 1, this.values, index, this._size - index); } this.keys[this._size] = null; this.values[this._size] = null; this.version++; } /// <summary> /// Removes the element with the specified key from a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <param name="key"> /// The key of the element to remove. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.SortedList" /> object is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.SortedList" /> has a fixed size. /// </exception> /// <filterpriority>1</filterpriority> public virtual void Remove(object key) { int num = this.IndexOfKey(key); if (num >= 0) { this.RemoveAt(num); } } /// <summary> /// Replaces the value at a specific index in a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <param name="index"> /// The zero-based index at which to save <paramref name="value" />. /// </param> /// <param name="value"> /// The <see cref="T:System.Object" /> to save into the <see cref="T:System.Collections.SortedList" /> object. The value can be null. /// </param> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="index" /> is outside the range of valid indexes for the <see cref="T:System.Collections.SortedList" /> object. /// </exception> /// <filterpriority>2</filterpriority> public virtual void SetByIndex(int index, object value) { if (index < 0 || index >= this._size) { throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index")); } this.values[index] = value; this.version++; } /// <summary> /// Returns a synchronized (thread-safe) wrapper for a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <returns> /// A synchronized (thread-safe) wrapper for the <see cref="T:System.Collections.SortedList" /> object. /// </returns> /// <param name="list"> /// The <see cref="T:System.Collections.SortedList" /> object to synchronize. /// </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="list" /> is null. /// </exception> /// <filterpriority>1</filterpriority> /// <PermissionSet> /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" /> /// </PermissionSet> [HostProtection(SecurityAction.LinkDemand, Synchronization = true)] public static SortedList Synchronized(SortedList list) { if (list == null) { throw new ArgumentNullException("list"); } return new SortedList.SyncSortedList(list); } /// <summary> /// Sets the capacity to the actual number of elements in a <see cref="T:System.Collections.SortedList" /> object. /// </summary> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.SortedList" /> object is read-only. /// /// -or- /// /// The <see cref="T:System.Collections.SortedList" /> has a fixed size. /// </exception> /// <filterpriority>2</filterpriority> public virtual void TrimToSize() { this.Capacity = this._size; } } }