Type System.Collections.ArrayList

extends System.Object

extends System.Collections.IList

extends System.Collections.ICollection

extends System.Collections.IEnumerable

extends System.ICloneable

Variables:

public virtual System.Int32 Capacity

public System.Int32 Count

public virtual System.Int32 Count

public virtual System.Boolean IsFixedSize

public System.Boolean IsFixedSize

public virtual System.Boolean IsReadOnly

public System.Boolean IsReadOnly

public virtual System.Boolean IsSynchronized

public System.Boolean IsSynchronized

public virtual System.Object Item

public virtual System.Object SyncRoot

public System.Object SyncRoot

Constructors:

public System.Collections.ArrayList()

Constructs and initializes a new instance of the System.Collections.ArrayList class that is empty and has the default initial System.Collections.ArrayList.Capacity .

The default initial System.Collections.ArrayList.Capacity of a System.Collections.ArrayList is 16.

public System.Collections.ArrayList(System.Int32 capacity)

Constructs and initializes a new instance of the System.Collections.ArrayList class that is empty and has the specified initial System.Collections.ArrayList.Capacity .

Parameter capacity: A System.Int32 that specifies the number of elements that the new instance is initially capable of storing.

Throws: : capacity < 0.

If capacity is zero, the System.Collections.ArrayList.Capacity of the current instance is set to 16 when the first element is added.

public System.Collections.ArrayList(System.Collections.ICollection c)

Constructs and initializes a new instance of the System.Collections.ArrayList class with the elements from the specified System.Collections.ICollection. The initial System.Collections.ArrayList.Count and System.Collections.ArrayList.Capacity of the new list are both equal to the number of elements in the specified collection.

Parameter c: The System.Collections.ICollection whose elements are copied to the new list.

Throws: : c is null.

The elements in the new System.Collections.ArrayList instance are in the same order as contained in c.

Functions:

public static System.Collections.ArrayList Adapter(System.Collections.IList list)

Creates a System.Collections.ArrayList that is a wrapper for the specified System.Collections.IList.

Parameter list: The System.Collections.IList to wrap.

Returns: The System.Collections.ArrayList wrapper for list.

Throws: : list is null.

This method returns a System.Collections.ArrayList that contains a reference to the System.Collections.IList list . Any modifications to the elements in either the returned list or list are reflected in the other. The System.Collections.ArrayList class provides generic System.Collections.ArrayList.Reverse, System.Collections.ArrayList.BinarySearch(System.Int32,System.Int32,System.Object,System.Collections.IComparer) and System.Collections.ArrayList.Sort methods. This wrapper provides a means to use those methods on System.Collections.IList list without implementing the methods for the list. Performing these operations through the wrapper may be less efficient than operations applied directly to the list.

public virtual System.Int32 Add(System.Object value)

Adds the specified System.Object to the end of the current instance.

Parameter value: The System.Object to be added to the end of the current instance.

Returns: A System.Int32 that specifies the index of the current instance at which value has been added.

Throws: : The current instance is read-only or has a fixed size.

As described above.

public virtual System.Void AddRange(System.Collections.ICollection c)

Adds the elements of the specified System.Collections.ICollection to the end of the current instance.

Parameter c: The System.Collections.ICollection whose elements are added to the end of the current instance.

Throws: : c is null.

Throws: : The current instance is read-only or has a fixed size.

As described above. If the System.Collections.ArrayList.Count of the current instance plus the size of the collection being added is greater than the System.Collections.ArrayList.Capacity of the current instance, the capacity of the current instance is either doubled or increased to the new System.Collections.ArrayList.Count, whichever is greater. The internal array is reallocated to accommodate the new elements and the existing elements are copied to the new array before the new elements are added. For the default implementation, if the current instance can accommodate the new elements without increasing the System.Collections.ArrayList.Capacity, this method is an O(n) operation, where n is the number of elements to be added. If the capacity needs to be increased to accommodate the new elements, this method becomes an O(n+m) operation, where n is the number of elements to be added and m is System.Collections.ArrayList.Count .

public virtual System.Int32 BinarySearch(System.Object value, System.Collections.IComparer comparer)

Searches the current instance for the specified System.Object using the specified System.Collections.IComparer implementation.

Parameter value: The System.Object for which to search.

Parameter comparer: The System.Collections.IComparer implementation to use when comparing elements. Specify null to use the System.IComparable implementation of each element.

Returns: A System.Int32 that specifies the results of the search as follows: If value is not found and the current instance is already sorted, the bitwise complement of the return value indicates the index in the current instance where value would be found.

Throws: : comparer is null, and both value and at least one element in the current instance do not implement the System.IComparable interface. -or- comparer is null, and value is not assignment-compatible with at least one element in the current instance.

A null reference can be compared with any type; therefore, comparisons with a null reference do not generate exceptions. A null reference evaluates to less than any non-null object, or equal to another null reference, when the two are compared. As described above. This method uses System.Array.BinarySearch(System.Array,System.Object) to search for value. value is compared to elements in a binary search of the current instance until an element with a value greater than or equal to value is found. If comparer is null, the System.IComparable implementation of the element being compared -- or of value if the element being compared does not implement the interface -- is used to make the comparison. If value does not implement the System.IComparable interface and is compared to an element that does not implement the interface either, System.ArgumentException is thrown. If the current instance is not already sorted, correct results are not guaranteed. For the default implementation, this method is an O(log n) operation where n is equal to the System.Collections.ArrayList.Count of the current instance.

public virtual System.Int32 BinarySearch(System.Object value)

Searches the current instance for the specified System.Object.

Parameter value: The System.Object for which to search.

Returns: A System.Int32 that specifies the results of the search as follows: If value is not found and the current instance is already sorted, the bitwise complement of the return value indicates the index in the current instance where value would be found.

Throws: : Both value and at least one element of the current instance do not implement the System.IComparable interface. -or- value is not assignment-compatible with at least one element in the current instance.

A null reference can be compared with any type; therefore, comparisons with a null reference do not generate exceptions. A null reference evaluates to less than any non-null object, or equal to another null reference, when the two are compared. As described above. This method uses System.Array.BinarySearch(System.Array,System.Object) to search for value. value is compared to elements in a binary search of the current instance until an element with a value greater than or equal to value is found. The System.IComparable implementation of the element being compared -- or of value if the element being compared does not implement the interface -- is used to make the comparison. If value does not implement the System.IComparable interface and is compared to an element that does not implement the interface either, System.ArgumentException is thrown. If the current instance is not already sorted, correct results are not guaranteed. For the default implementation, this method is an O(log n) operation where n is equal to the System.Collections.ArrayList.Count of the current instance.

public virtual System.Int32 BinarySearch(System.Int32 index, System.Int32 count, System.Object value, System.Collections.IComparer comparer)

Searches the specified range in the current instance for the specified System.Object using the specified System.Collections.IComparer implementation.

Parameter index: A System.Int32 that specifies the index at which searching starts. This value is greater than or equal to zero, and less than the System.Collections.ArrayList.Count of the current instance.

Parameter count: A System.Int32 that specifies the number of elements to search, beginning with index . This value is greater than or equal to zero, and less than or equal to the System.Collections.ArrayList.Count of the current instance minus index.

Parameter value: The System.Object for which to search.

Parameter comparer: The System.Collections.IComparer implementation to use when comparing elements. Specify null to use the System.IComparable implementation of each element.

Returns: A System.Int32 that specifies the results of the search as follows: If value is not found and the current instance is already sorted, the bitwise complement of the return value indicates the index in the current instance where value would be found in the range of index to index + count - 1.

Throws: : System.Collections.ArrayList.Count of the current instance - index < count. -or- comparer is null, and both value and at least one element of the current instance do not implement the System.IComparable interface. -or- comparer is null, and value is not assignment-compatible with at least one element in the current instance.

Throws: : index < 0. -or- count < 0.

A null reference can be compared with any type; therefore, comparisons with a null reference do not generate exceptions. A null reference evaluates to less than any non-null object, or equal to another null reference, when the two are compared. As described above. This method uses System.Array.BinarySearch(System.Array,System.Object) to search for value . value is compared to elements in a binary search of the range of index to index + count - 1 in the current instance until an element with a value greater than or equal to value is found or the end of the range is reached. If comparer is null , the System.IComparable implementation of the element being compared -- or of value if the element being compared does not implement the interface -- is used to make the comparison. If value does not implement the System.IComparable interface and is compared to an element that does not implement the interface either, System.ArgumentException is thrown. If the current instance is not already sorted, correct results are not guaranteed. For the default implementation, this method is an O(log count) operation.

public virtual System.Void Clear()

Sets the elements in the current instance to zero, false, or null, depending upon the element type.

Throws: : The current instance is read-only or has a fixed size.

This method is implemented to support the System.Collections.IList interface. Reference-type elements are set to null. Value-type elements are set to zero, except for System.Boolean elements, which are set to false. This method uses System.Array.Clear(System.Array,System.Int32,System.Int32) to reset the values of the current instance. System.Collections.ArrayList.Count is set to zero. System.Collections.ArrayList.Capacity is not changed. To reset the System.Collections.ArrayList.Capacity of the current instance, call System.Collections.ArrayList.TrimToSize or set the System.Collections.ArrayList.Capacity property directly.

public virtual System.Object Clone()

Returns a System.Object that is a copy of the current instance.

Returns: A System.Object that is a copy of the current instance.

This method is implemented to support the System.ICloneable interface. As described above. This method uses System.Array.Copy(System.Array,System.Array,System.Int32) to clone the current instance.

public virtual System.Boolean Contains(System.Object item)

Determines whether the specified System.Object is contained in the current instance.

Parameter item: The System.Object to locate in the current instance.

Returns: true if item is contained in the current instance; otherwise, false.

This method is implemented to support the System.Collections.IList interface. As described above. This method determines equality by calling the System.Object.Equals(System.Object) implementation of the type of item. For the default implementation, this method is an O(n) operation where n is equal to the System.Collections.ArrayList.Count of the current instance. If the current instance is sorted, it is more efficient to call System.Collections.ArrayList.BinarySearch(System.Int32,System.Int32,System.Object,System.Collections.IComparer) method.

public virtual System.Void CopyTo(System.Int32 index, System.Array array, System.Int32 arrayIndex, System.Int32 count)

Copies the specified range of elements from the current instance to the specified System.Array, starting at the specified index of the array.

Parameter index: A System.Int32 that specifies the index in the current instance at which copying begins. This value is greater than or equal to 0, and less than the System.Collections.ArrayList.Count of the current instance.

Parameter array: The one-dimensional System.Array that is the destination of the elements copied from the current instance.

Parameter arrayIndex: A System.Int32 that specifies the first index of array to which the elements of the current instance are copied. This value is greater than or equal to zero, and less than array.Length minus count.

Parameter count: A System.Int32 that specifies the number of elements to copy. This value is greater than or equal to 0, and less than both the System.Collections.ArrayList.Count of the current instance minus index and array.Length minus arrayIndex.

Throws: : array is null.

Throws: : index < 0. -or- arrayIndex < 0. -or- count < 0.

Throws: : array has more than one dimension. -or- index >= System.Collections.ArrayList.Count of the current instance . -or- count >= System.Collections.ArrayList.Count of the current instance - index. -or- count >= array.Length - arrayIndex.

Throws: : At least one element of the current instance is not assignment-compatible with the type of array.

As described above. This method uses System.Array.Copy(System.Array,System.Array,System.Int32) to copy the current instance to array.

public virtual System.Void CopyTo(System.Array array)

Copies the elements from the current instance to the specified System.Array.

Parameter array: The one-dimensional System.Array that is the destination of the elements copied from the current instance. The System.Array.Length of this array is greater than or equal to the System.Collections.ArrayList.Count of the current instance.

Throws: : array is null.

Throws: : array has more than one dimension. -or- System.Collections.ArrayList.Count of the current instance > array.Length.

Throws: : At least one element in the current instance is not assignment-compatible with the type of array.

As described above. This method uses System.Array.Copy(System.Array,System.Array,System.Int32) to copy the current instance to array.

public virtual System.Void CopyTo(System.Array array, System.Int32 arrayIndex)

Copies the elements from the current instance to the specified System.Array , starting at the specified index of the array.

Parameter array: The one-dimensional System.Array that is the destination of the elements copied from the current instance. The System.Array.Length of this array is greater than or equal to the sum of arrayIndex and the System.Collections.ArrayList.Count of the current instance.

Parameter arrayIndex: A System.Int32 that specifies the first index of array to which the elements of the current instance are copied. This value is greater than or equal to zero, and less than array.Length.

Throws: : array is null.

Throws: : arrayIndex < 0.

Throws: : array has more than one dimension. -or- arrayIndex >= array.Length. -or- arrayIndex + System.Collections.ArrayList.Count of the current instance > array.Length.

Throws: : At least one element in the current instance is not assignment-compatible with the type of array.

This method is implemented to support the System.Collections.IList interface. As described above. This method uses System.Array.Copy(System.Array,System.Array,System.Int32) to copy the current instance to array.

public static System.Collections.ArrayList FixedSize(System.Collections.ArrayList list)

Returns a System.Collections.ArrayList wrapper with a fixed size.

Parameter list: The System.Collections.ArrayList to wrap.

Returns: A System.Collections.ArrayList wrapper with a fixed size.

Throws: : list is null.

This method returns a fixed-size System.Collections.ArrayList that contains a reference to list. Operations that attempt add to or delete from this new list will throw System.NotSupportedException. Any modifications of the elements in either the returned list or list will be reflected in the other. The System.Collections.ArrayList.IsFixedSize property of the new list is true. Every other property value of the new list references the same property value of list. By performing operations on the new list, this wrapper can be used to prevent additions to and deletions from the System.Collections.ArrayList list. The elements of the list can still be modified by operations on the returned list.

public virtual System.Collections.IEnumerator GetEnumerator()

Returns a System.Collections.IEnumerator for the current instance.

Returns: A System.Collections.IEnumerator for the current instance.

If the the current instance is modified while an enumeration is in progress, a call to System.Collections.IEnumerator.MoveNext or System.Collections.IEnumerator.Reset throws System.InvalidOperationException . For detailed information regarding the use of an enumerator, see System.Collections.IEnumerator. This property is implemented to support the System.Collections.IList interface. As described above.

public virtual System.Collections.IEnumerator GetEnumerator(System.Int32 index, System.Int32 count)

Returns a System.Collections.IEnumerator for the specified section of the current instance.

Parameter index: A System.Int32 that specifies the index of the current instance before which the enumerator is initially placed. This value is greater than or equal to 0, and less than the System.Collections.ArrayList.Count of the current instance.

Parameter count: A System.Int32 that specifies the number of elements, beginning with index , in the current instance over which the enumerator can iterate. This value is greater than or equal to 0, and less than or equal to the System.Collections.ArrayList.Count of the current instance minus index .

Returns: A System.Collections.IEnumerator that can iterate over the range of index to index + count - 1 in the current instance.

Throws: : index < 0. -or- count < 0.

Throws: : index + count > System.Collections.ArrayList.Count of the current instance.

The enumerator only enumerates over the range of the current instance from index to index + count - 1. If the current instance is modified while an enumeration is in progress, a call to System.Collections.IEnumerator.MoveNext or System.Collections.IEnumerator.Reset will throw System.InvalidOperationException . For detailed information regarding the use of an enumerator, see System.Collections.IEnumerator. As described above. The enumerator is initially placed just before the element at position index in the current instance. A call to System.Collections.IEnumerator.Reset returns the enumerator to this position. If the enumerator is positioned over an element in the range of index -1 to index + count, and the elements of the current instance have not been modified while the enumeration was in progress, a call to System.Collections.IEnumerator.MoveNext either returns true and advances the enumerator one element in the current instance, or returns false and leaves the enumerator in its current position.

public virtual System.Collections.ArrayList GetRange(System.Int32 index, System.Int32 count)

Returns a System.Collections.ArrayList that represents the specified range of the current instance.

Parameter index: A System.Int32 that specifies the zero-based index in the current instance at which the range starts. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus count , inclusive.

Parameter count: A System.Int32 that specifies the number of elements in the range. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus index , inclusive.

Returns: A System.Collections.ArrayList that represents the range in the current instance from index to index + count - 1.

Throws: : index < 0. -or- count < 0.

Throws: : System.Collections.ArrayList.Count of the current instance - index < count.

As described above. This method does not create copies of the elements: the new System.Collections.ArrayList instance is a view window into the source list. Therefore, all subsequent changes to the source list must be done through this view window System.Collections.ArrayList . If changes are made directly to the source list, the view window list is invalidated and any operations on it throw System.InvalidOperationException .

public virtual System.Int32 IndexOf(System.Object value)

Searches the current instance, returning the index of the first occurrence of the specified System.Object.

Parameter value: The System.Object to locate in current instance.

Returns: A System.Int32 that specifies the index of the first occurrence of value in the current instance, if found; otherwise, -1. This provides the caller with a standard code for a failed search.

This method is implemented to support the System.Collections.IList interface. As described above. This method uses System.Array.IndexOf(System.Array,System.Object) to search the current instance for value . For the default implementation, this method performs a linear search. On average, this is an O(n/2) operation, where n is count. The longest search is an O(n) operation.

public virtual System.Int32 IndexOf(System.Object value, System.Int32 startIndex, System.Int32 count)

Searches the current instance, returning the index of the first occurrence of the specified System.Object in the specified range.

Parameter value: The System.Object to locate in current instance.

Parameter startIndex: A System.Int32 that specifies the index at which to begin searching. This value is greater than or equal to zero, and less than the System.Collections.ArrayList.Count of the current instance.

Parameter count: A System.Int32 that specifies the number of elements to search. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus startIndex , inclusive.

Returns: A System.Int32 that specifies the index of the first occurrence of value in the current instance, within the range startIndex to startIndex + count - 1, if found; otherwise, -1. This provides the caller with a standard code for a failed search.

Throws: : startIndex>= System.Collections.ArrayList.Count of the current instance. -or- count < 0. -or- count >System.Collections.ArrayList.Count of the current instance - startIndex.

As described above. This method uses System.Array.IndexOf(System.Array,System.Object) to search the current instance for value. For the default implementation, this method performs a linear search. On average, this is an O(n/2) operation, where n is count. The longest search is an O(n) operation.

public virtual System.Int32 IndexOf(System.Object value, System.Int32 startIndex)

Searches the current instance, returning the index of the first occurrence of the specified System.Object in the specified range.

Parameter value: The System.Object to locate in current instance.

Parameter startIndex: A System.Int32 that specifies the index at which searching begins. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus 1, inclusive.

Returns: A System.Int32 that specifies the index of the first occurrence of value in the current instance, if found within the range startIndex to the end of the current instance; otherwise, -1. This provides the caller with a standard code for a failed search.

Throws: : startIndex < 0. -or- startIndex >= System.Collections.ArrayList.Count of the current instance.

As described above. This method uses System.Array.IndexOf(System.Array,System.Object) to search the current instance for value. For the default implementation, this method performs a linear search. On average, this is an O(n/2) operation, where n is count. The longest search is an O(n) operation.

public virtual System.Void Insert(System.Int32 index, System.Object value)

Inserts the specified System.Object into the current instance at the specified index.

Parameter index: A System.Int32 that specifies the index in the current instance at which value is inserted. This value is between 0 and the System.Collections.ArrayList.Count of the current instance, inclusive.

Parameter value: The System.Object to insert.

Throws: : index < 0. -or- index > System.Collections.ArrayList.Count of the current instance.

Throws: : The current instance is read-only or has a fixed size.

This method is implemented to support the System.Collections.IList interface. As described above. If the System.Collections.ArrayList.Count of the current instance is equal to the System.Collections.ArrayList.Capacity of the current instance, the capacity of the list is doubled by automatically reallocating the internal array before the new element is inserted. If index is equal to the System.Collections.ArrayList.Count of the current instance, value is added to the end of the current instance.

public virtual System.Void InsertRange(System.Int32 index, System.Collections.ICollection c)

Inserts the elements of the specified System.Collections.ICollection at the specified index of the current instance.

Parameter index: A System.Int32 that specifies the index in the current instance at which the new elements are inserted. This value is between 0 and the System.Collections.ArrayList.Count of the current instance, inclusive.

Parameter c: The System.Collections.ICollection whose elements are inserted into the current instance.

Throws: : c is null.

Throws: : index < 0. index > System.Collections.ArrayList.Count of the current instance.

Throws: : The current instance is read-only or has a fixed size.

As described above. If the System.Collections.ArrayList.Count of the current instance plus the size of System.Collections.ICollection c is greater than the System.Collections.ArrayList.Capacity of the current instance, the capacity of the current instance is either doubled or increased to the new count, whichever yields a greater capacity. The internal array is reallocated to accommodate the new elements. If index is equal to the System.Collections.ArrayList.Count of the current instance, the elements of c are added to the end of the current instance. The order of the elements in the System.Collections.ICollection c is preserved in the current instance.

public virtual System.Int32 LastIndexOf(System.Object value)

Searches the current instance, returning the index of the last occurrence of the specified System.Object.

Parameter value: The System.Object to locate in the current instance.

Returns: A System.Int32 that specifies the index of the last occurrence of value in the current instance, if found; otherwise, -1. This provides the caller with a standard code for a failed search.

As described above. This method uses System.Array.LastIndexOf(System.Array,System.Object) to search the current instance for value. For the default implementation, this method performs a linear search. On average, this is an O(n/2) operation, where n is System.Collections.ArrayList.Count of the current instance. The longest search is an O(n) operation.

public virtual System.Int32 LastIndexOf(System.Object value, System.Int32 startIndex)

Searches the current instance, returning the index of the last occurrence of the specified System.Object in the specified range of the current instance.

Parameter value: The System.Object to locate in the current instance.

Parameter startIndex: A System.Int32 that specifies the index at which searching starts. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus 1, inclusive.

Returns: A System.Int32 that specifies the index of the last occurrence of value in the range of startIndex through the first element of the current instance, if found; otherwise, -1. This provides the caller with a standard code for a failed search.

Throws: : startIndex < 0. -or- startIndex >= System.Collections.ArrayList.Count of the current instance.

As described above. This method uses System.Array.LastIndexOf(System.Array,System.Object) to search the current instance for value. For the default implementation, this method performs a linear search. On average, this is an O(count/2) operation. The longest search is an O(count) operation.

public virtual System.Int32 LastIndexOf(System.Object value, System.Int32 startIndex, System.Int32 count)

Searches the current instance, returning the index of the last occurrence of the specified System.Object in the specified range.

Parameter value: The System.Object to locate in the current instance.

Parameter startIndex: A System.Int32 that specifies the index at which searching starts.

Parameter count: A System.Int32 that specifies the number of elements to search, beginning with startIndex .

Returns: A System.Int32 that specifies the index of the last occurrence of value in the current instance, within the range startIndex through startIndex - count + 1, if found; otherwise, -1. This provides the caller with a standard code for a failed search.

Throws: : startIndex < 0. -or- count < 0. -or- startIndex >= System.Collections.ArrayList.Count of the current instance. -or- count >= System.Collections.ArrayList.Count of the current instance. -or- count > startIndex + 1.

As described above. This method uses System.Array.LastIndexOf(System.Array,System.Object) to search the current instance for value. For the default implementation, this method performs a linear search. On average, this is an O(count/2) operation. The longest search is an O(count) operation.

public static System.Collections.ArrayList ReadOnly(System.Collections.ArrayList list)

Returns a read-only System.Collections.ArrayList wrapper.

Parameter list: The System.Collections.ArrayList to wrap.

Returns: A read-only System.Collections.ArrayList wrapper around list.

Throws: : list is null.

This method returns a read-only System.Collections.ArrayList that contains a reference to list. Operations that attempt add to, delete from, or modify the elements of this new list will throw System.NotSupportedException. Any modifications of the elements list will be reflected in the new list. The System.Collections.ArrayList.IsReadOnly and System.Collections.ArrayList.IsFixedSize properties of the new list are true. Every other property value of the new list references the same property value of list. By performing operations on the new list, this wrapper can be used to prevent additions to, deletions from, and modifications of the System.Collections.ArrayList list.

public virtual System.Void Remove(System.Object obj)

Removes the first occurrence of the specified System.Object from the current instance.

Parameter obj: The System.Object to remove from the current instance.

Throws: : The current instance is read-only or has a fixed size.

This method is implemented to support the System.Collections.IList interface. As described above. This method determines equality by calling System.Object.Equals(System.Object). If and only if obj is found in the current instance, obj is removed from the current instance, the rest of the elements are shifted down to fill the position vacated by obj, the System.Collections.ArrayList.Count of the current instance is decreased by one, and the position that was previously the last element in the current instance is set to null. If obj is not found in the current instance, the current instance remains unchanged. For the default implementation, this method performs a linear search. On average, this is an O(n/2) operation, where n is System.Collections.ArrayList.Count of the current instance. The longest search is an O(n) operation.

public virtual System.Void RemoveAt(System.Int32 index)

Removes the element at the specified index from the current instance.

Parameter index: A System.Int32 that specifies the zero-based index of the element to remove from the current instance. This value is between 0 and the System.Collections.ArrayList.Count of the current instance, inclusive.

Throws: : index < 0. -or- index >= System.Collections.ArrayList.Count of the current instance.

Throws: : The current instance is read-only or has a fixed size.

This method is implemented to support the System.Collections.IList interface. As described above. The element at position index is removed from the current instance, the rest of the elements are shifted down to fill the position vacated by that element, the System.Collections.ArrayList.Count of the current instance is decreased by one, and the position that was previously the last element in the current instance is set to null.

public virtual System.Void RemoveRange(System.Int32 index, System.Int32 count)

Removes the specified range of elements from the current instance.

Parameter index: A System.Int32 that specifies the zero-based index of the first element of the range of elements in the current instance to remove. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus count , inclusive.

Parameter count: A System.Int32 that specifies the number of elements to remove. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus index , inclusive.

Throws: : index < 0. -or- count < 0.

Throws: : System.Collections.ArrayList.Count of the current instance - index < count.

Throws: : The current instance is read-only or has a fixed size.

As described above. The elements in the range of index to index + count - 1 are removed from the current instance, the rest of the elements are shifted down to fill the position vacated by those elements, the System.Collections.ArrayList.Count of the current instance is decreased by count, and the count positions that were previously the last elements in the current instance are set to null .

public static System.Collections.ArrayList Repeat(System.Object value, System.Int32 count)

Returns a new System.Collections.ArrayList whose elements are copies of the specified System.Object.

Parameter value: The System.Object used to initialize the new System.Collections.ArrayList instance.

Parameter count: A System.Int32 that specifies the number of times value is copied into the new System.Collections.ArrayList instance.

Returns: A new System.Collections.ArrayList with count number of elements, all of which are copies of value.

Throws: : count < 0.

If count is less than the default initial capacity, 16, the System.Collections.ArrayList.Capacity of the new System.Collections.ArrayList instance is set to the default initial capacity. Else, the capacity is set to count . The System.Collections.ArrayList.Count of the new instance is set to count.

public virtual System.Void Reverse()

Reverses the sequence of the elements in the current instance.

Throws: : The current instance is read-only.

As described above. This method uses System.Array.Reverse(System.Array) to modify the ordering of the elements in the current instance.

public virtual System.Void Reverse(System.Int32 index, System.Int32 count)

Reverses the sequence of the elements in the specified range of the current instance.

Parameter index: A System.Int32 that specifies the zero-based index in the current instance at which reversing starts. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus count , inclusive.

Parameter count: A System.Int32 that specifies the number of elements to reverse. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus index , inclusive.

Throws: : index < 0. -or- count < 0.

Throws: : System.Collections.ArrayList.Count of the current instance - index < count.

Throws: : The current instance is read-only.

As described above. This method uses System.Array.Reverse(System.Array) to modify the ordering of the current instance.

public virtual System.Void SetRange(System.Int32 index, System.Collections.ICollection c)

Copies the elements of the specified System.Collections.ICollection to a range in the current instance.

Parameter index: A System.Int32 that specifies the zero-based index in the current instance at which to start copying the elements of c. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus c.Count, inclusive.

Parameter c: The System.Collections.ICollection whose elements to copy to the current instance.

Throws: : index < 0. -or- System.Collections.ArrayList.Count of the current instance - index < c.Count.

Throws: : c is null.

Throws: : The current instance is read-only.

As described above. This method uses the System.Collections.ICollection.CopyTo(System.Array,System.Int32) implementation of System.Collections.ICollection c.

public virtual System.Void Sort(System.Int32 index, System.Int32 count, System.Collections.IComparer comparer)

Sorts the elements in the specified range of the current instance using the specified System.Collections.IComparer implementation.

Parameter index: A System.Int32 that specifies the zero-based index at which sorting starts. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus count , inclusive.

Parameter count: A System.Int32 that specifies the number of elements to sort. This value is between 0 and the System.Collections.ArrayList.Count of the current instance minus index , inclusive.

Parameter comparer: The System.Collections.IComparer implementation to use when comparing elements. Specify null to use the System.IComparable implementation of each element in the current instance.

Throws: : index < 0. -or- count < 0.

Throws: : System.Collections.ArrayList.Count of the current instance - index < count.

Throws: : comparer is null, and one or more elements in the current instance do not implement the System.IComparable interface.

Throws: : The current instance is read-only.

As described above. If comparer is null, the System.IComparable implementation of each element in the current instance is used to make the sorting comparisons. If the sort is not successfully completed, the results are unspecified. For the default implementation, this method uses System.Array.Sort(System.Array), which uses the Quicksort algorithm. This is an O(n log n) operation, where n is the number of elements to sort.

public virtual System.Void Sort(System.Collections.IComparer comparer)

Sorts the elements of current instance using the specified System.Collections.IComparer.

Parameter comparer: The System.Collections.IComparer implementation to use when comparing elements. Specify null to use the System.IComparable implementation of each element in the current instance.

Throws: : comparer is null, and one or more elements in the current instance do not implement the System.IComparable interface.

Throws: : The current instance is read-only.

As described above. If comparer is null, the System.IComparable implementation of each element in the current instance is used to make the sorting comparisons. If the sort is not successfully completed, the results are unspecified. For the default implementation, this method uses System.Array.Sort(System.Array), which uses the Quicksort algorithm. This is an O(n log n) operation, where n is the number of elements to sort.

public virtual System.Void Sort()

Sorts the elements of the current instance.

Throws: : One or more elements in the current instance do not implement the System.IComparable interface.

Throws: : The current instance read-only.

As described above. The System.IComparable implementation of each element in the current instance is used to make the sorting comparisons. If the sort is not successfully completed, the results are unspecified. For the default implementation, this method uses System.Array.Sort(System.Array), which uses the Quicksort algorithm. This is an O(n log n) operation, where n is the number of elements to sort.

public static System.Collections.ArrayList Synchronized(System.Collections.ArrayList list)

Returns a System.Collections.ArrayList wrapper around the specified System.Collections.ArrayList that is synchronized (thread-safe).

Parameter list: The System.Collections.ArrayList to synchronize.

Returns: A System.Collections.ArrayList wrapper that is synchronized (thread-safe).

Throws: : list is null.

This method returns a thread-safe System.Collections.ArrayList that contains a reference to list. Any modifications of the elements in either the returned list or list will be reflected in the other. The System.Collections.ArrayList.IsSynchronized property of the new list is true. Every other property value of the new list references the same property value of list. By performing operations on the new list, this wrapper can be used to guarantee thread-safe access to the System.Collections.ArrayList list.

public virtual System.Object[] ToArray()

Copies the elements of the current instance to a new System.Object array.

Returns: A System.Object array containing copies of the elements of the current instance.

As described above. The elements are copied using System.Array.Copy(System.Array,System.Array,System.Int32). For the default implementation, this method is an O(n) operation, where n is the System.Collections.ArrayList.Count of the current instance.

public virtual System.Array ToArray(System.Type type)

Copies the elements of the current instance to a new array of the specified System.Type.

Parameter type: The System.Type of the System.Array to create and copy the elements of the current instance.

Returns: An array of System.Type type containing copies of the elements of the current instance.

Throws: : type is null.

Throws: : At least one element of the current instance cannot be cast to the System.Type type.

As described above. The elements are copied using System.Array.Copy(System.Array,System.Array,System.Int32). For the default implementation, this method is an O(n) operation, where n is the System.Collections.ArrayList.Count of the current instance.

public virtual System.Void TrimToSize()

Sets the System.Collections.ArrayList.Capacity of the current instance to the System.Collections.ArrayList.Count of the current instance.

Throws: : The current instance is read-only or has a fixed size.

This method can be used to minimize the memory overhead of the current instance if no new elements will be added to it. To completely clear all elements from the current instance, call the System.Collections.ArrayList.Clear method before calling System.Collections.ArrayList.TrimToSize. As described above. If the System.Collections.ArrayList.Count of the current instance is zero, the System.Collections.ArrayList.Capacity of the current instance is set to the default initial capacity of 16.

Functions inherited from System.Object:

public virtual System.Boolean Equals(System.Object obj)

Determines whether the specified System.Object is equal to the current instance.

Parameter obj: The System.Object to compare with the current instance.

Returns: true if obj is equal to the current instance; otherwise, false.

The statements listed below are required to be true for all implementations of the System.Object.Equals(System.Object) method. In the list, x, y, and z represent non-null object references. See System.Object.GetHashCode for additional required behaviors pertaining to the System.Object.Equals(System.Object) method. Implementations of System.Object.Equals(System.Object) should not throw exceptions. The System.Object.Equals(System.Object) method tests for referential equality , which means that System.Object.Equals(System.Object) returns true if the specified instance of Object and the current instance are the same instance; otherwise, it returns false . An implementation of the System.Object.Equals(System.Object) method is shown in the following C# code: public virtual bool Equals(Object obj) { return this == obj; } For some kinds of objects, it is desirable to have System.Object.Equals(System.Object) test for value equality instead of referential equality. Such implementations of Equals return true if the two objects have the same "value", even if they are not the same instance. The definition of what constitutes an object's "value" is up to the implementer of the type, but it is typically some or all of the data stored in the instance variables of the object. For example, the value of a System.String is based on the characters of the string; the Equals method of the System.String class returns true for any two string instances that contain exactly the same characters in the same order. When the Equals method of a base class provides value equality, an override of Equals in a class derived from that base class should invoke the inherited implementation of Equals . It is recommended (but not required) that types overriding System.Object.Equals(System.Object) also override System.Object.GetHashCode. Hashtables cannot be relied on to work correctly if this recommendation is not followed. If your programming language supports operator overloading, and if you choose to overload the equality operator for a given type, that type should override the Equals method. Such implementations of the Equals method should return the same results as the equality operator. Following this guideline will help ensure that class library code using Equals (such as System.Collections.ArrayList and System.Collections.Hashtable ) behaves in a manner that is consistent with the way the equality operator is used by application code. If you are implementing a value type, you should follow these guidelines: For reference types, the guidelines are as follows: If you implement System.IComparable on a given type, you should override Equals on that type. The System.Object.Equals(System.Object) method is called by methods in collections classes that perform search operations, including the System.Array.IndexOf(System.Array,System.Object) method and the System.Collections.ArrayList.Contains(System.Object) method.

Example:

Example 1:

The following example contains two calls to the default implementation of System.Object.Equals(System.Object) .

using System;
class MyClass {
   static void Main() {
      Object obj1 = new Object();
      Object obj2 = new Object();
      Console.WriteLine(obj1.Equals(obj2));
      obj1 = obj2; 
      Console.WriteLine(obj1.Equals(obj2)); 
   }
}

The output is

False

True

Example 2:

The following example shows a Point class that overrides the System.Object.Equals(System.Object) method to provide value equality and a class Point3D, which is derived from Point . Because Point's override of System.Object.Equals(System.Object) is the first in the inheritance chain to introduce value equality, the Equals method of the base class (which is inherited from System.Object and checks for referential equality) is not invoked. However, Point3D.Equals invokes Point.Equals because Point implements Equals in a manner that provides value equality.

using System;
public class Point: object {
 int x, y;
 public override bool Equals(Object obj) {
 //Check for null and compare run-time types.
 if (obj == null || GetType() != obj.GetType()) return false;
 Point p = (Point)obj;
 return (x == p.x) && (y == p.y);
 }
 public override int GetHashCode() {
 return x ^ y;
 }
}

class Point3D: Point {
 int z;
 public override bool Equals(Object obj) {
 return base.Equals(obj) && z == ((Point3D)obj).z;
 }
 public override int GetHashCode() {
 return base.GetHashCode() ^ z;
 }
}

The Point.Equals method checks that the obj argument is non-null and that it references an instance of the same type as this object. If either of those checks fail, the method returns false. The System.Object.Equals(System.Object) method uses System.Object.GetType to determine whether the run-time types of the two objects are identical. (Note that typeof is not used here because it returns the static type.) If instead the method had used a check of the form

<doc:param name="obj"/>
is Point , the check would return true in cases where obj is an instance of a subclass of Point , even though obj and the current instance are not of the same runtime type. Having verified that both objects are of the same type, the method casts obj to type Point and returns the result of comparing the instance variables of the two objects.

In Point3D.Equals , the inherited Equals method is invoked before anything else is done; the inherited Equals method checks to see that obj is non-null, that obj is an instance of the same class as this object, and that the inherited instance variables match. Only when the inherited Equals returns true does the method compare the instance variables introduced in the subclass. Specifically, the cast to Point3D is not executed unless obj has been determined to be of type Point3D or a subclass of Point3D .

Example 3:

In the previous example, operator == (the equality operator) is used to compare the individual instance variables. In some cases, it is appropriate to use the System.Object.Equals(System.Object) method to compare instance variables in an Equals implementation, as shown in the following example:

using System;
class Rectangle {
 Point a, b;
 public override bool Equals(Object obj) {
 if (obj == null || GetType() != obj.GetType()) return false;
 Rectangle r = (Rectangle)obj;
 //Use Equals to compare instance variables
 return a.Equals(r.a) && b.Equals(r.b);
 }
 public override int GetHashCode() {
 return a.GetHashCode() ^ b.GetHashCode();
 }
}

Example 4:

In some languages, such as C#, operator overloading is supported. When a type overloads operator ==, it should also override the System.Object.Equals(System.Object) method to provide the same functionality. This is typically accomplished by writing the Equals method in terms of the overloaded operator ==. For example:

using System;
public struct Complex {
 double re, im;
 public override bool Equals(Object obj) {
 return obj is Complex && this == (Complex)obj;
 }
 public override int GetHashCode() {
 return re.GetHashCode() ^ im.GetHashCode();
 }
 public static bool operator ==(Complex x, Complex y) {
 return x.re == y.re && x.im == y.im;
 }
 public static bool operator !=(Complex x, Complex y) {
 return !(x == y);
 }
}

Because Complex is a C# struct (a value type), it is known that there will be no subclasses of Complex . Therefore, the System.Object.Equals(System.Object) method need not compare the GetType() results for each object, but can instead use the is operator to check the type of the obj parameter.

public static System.Boolean Equals(System.Object objA, System.Object objB)

Determines whether two object references are equal.

Parameter objA: First object to compare.

Parameter objB: Second object to compare.

Returns: true if one or more of the following statements is true: otherwise returns false.

This static method checks for null references before it calls objA.Equals(objB ) and returns false if either objA or objB is null. If the Equals(object obj) implementation throws an exception, this method throws an exception.

Example:

The following example demonstrates the System.Object.Equals(System.Object) method.

using System;

public class MyClass {
   public static void Main() {
   string s1 = "Tom";
   string s2 = "Carol";
   Console.WriteLine("Object.Equals(\"{0}\", \"{1}\") => {2}", 
      s1, s2, Object.Equals(s1, s2));

   s1 = "Tom";
   s2 = "Tom";
   Console.WriteLine("Object.Equals(\"{0}\", \"{1}\") => {2}", 
      s1, s2, Object.Equals(s1, s2));

   s1 = null;
   s2 = "Tom";
   Console.WriteLine("Object.Equals(null, \"{1}\") => {2}",
       s1, s2, Object.Equals(s1, s2));

   s1 = "Carol";
   s2 = null;
   Console.WriteLine("Object.Equals(\"{0}\", null) => {2}", 
       s1, s2, Object.Equals(s1, s2));

   s1 = null;
   s2 = null;
   Console.WriteLine("Object.Equals(null, null) => {2}", 
       s1, s2, Object.Equals(s1, s2));
   }
}
   

The output is

Object.Equals("Tom", "Carol") => False

Object.Equals("Tom", "Tom") => True

Object.Equals(null, "Tom") => False

Object.Equals("Carol", null) => False

Object.Equals(null, null) => True

public System.Void Finalize()

Allows a System.Object to perform cleanup operations before the memory allocated for the System.Object is automatically reclaimed.

During execution, System.Object.Finalize is automatically called after an object becomes inaccessible, unless the object has been exempted from finalization by a call to System.GC.SuppressFinalize(System.Object). During shutdown of an application domain, System.Object.Finalize is automatically called on objects that are not exempt from finalization, even those that are still accessible. System.Object.Finalize is automatically called only once on a given instance, unless the object is re-registered using a mechanism such as System.GC.ReRegisterForFinalize(System.Object) and System.GC.SuppressFinalize(System.Object) has not been subsequently called. Conforming implementations of the CLI are required to make every effort to ensure that for every object that has not been exempted from finalization, the System.Object.Finalize method is called after the object becomes inaccessible. However, there may be some circumstances under which Finalize is not called. Conforming CLI implementations are required to explicitly specify the conditions under which Finalize is not guaranteed to be called. For example, Finalize might not be guaranteed to be called in the event of equipment failure, power failure, or other catastrophic system failures. In addition to System.GC.ReRegisterForFinalize(System.Object) and System.GC.SuppressFinalize(System.Object), conforming implementations of the CLI are allowed to provide other mechanisms that affect the behavior of System.Object.Finalize . Any mechanisms provided are required to be specified by the CLI implementation. The order in which the Finalize methods of two objects are run is unspecified, even if one object refers to the other. The thread on which Finalize is run is unspecified. Every implementation of System.Object.Finalize in a derived type is required to call its base type's implementation of Finalize . This is the only case in which application code calls System.Object.Finalize . The System.Object.Finalize implementation does nothing. A type should implement Finalize when it uses unmanaged resources such as file handles or database connections that must be released when the managed object that uses them is reclaimed. Because Finalize methods may be invoked in any order (including from multiple threads), synchronization may be necessary if the Finalize method may interact with other objects, whether accessible or not. Furthermore, since the order in which Finalize is called is unspecified, implementers of Finalize (or of destructors implemented through overriding Finalize) must take care to correctly handle references to other objects, as their Finalize method may already have been invoked. In general, referenced objects should not be considered valid during finalization. See the System.IDisposable interface for an alternate means of disposing of resources. For C# developers: Destructors are the C# mechanism for performing cleanup operations. Destructors provide appropriate safeguards, such as automatically calling the base type's destructor. In C# code, System.Object.Finalize cannot be called or overridden.

public virtual System.Int32 GetHashCode()

Generates a hash code for the current instance.

Returns: A System.Int32 containing the hash code for the current instance.

System.Object.GetHashCode serves as a hash function for a specific type. A hash function is used to quickly generate a number (a hash code) corresponding to the value of an object. Hash functions are used with hashtables. A good hash function algorithm rarely generates hash codes that collide. For more information about hash functions, see The Art of Computer Programming , Vol. 3, by Donald E. Knuth. All implementations of System.Object.GetHashCode are required to ensure that for any two object references x and y, if x.Equals(y) == true, then x.GetHashCode() == y.GetHashCode(). Hash codes generated by System.Object.GetHashCode need not be unique. Implementations of System.Object.GetHashCode are not permitted to throw exceptions. The System.Object.GetHashCode implementation attempts to produce a unique hash code for every object, but the hash codes generated by this method are not guaranteed to be unique. Therefore, System.Object.GetHashCode may generate the same hash code for two different instances. It is recommended (but not required) that types overriding System.Object.GetHashCode also override System.Object.Equals(System.Object) . Hashtables cannot be relied on to work correctly if this recommendation is not followed. Use this method to obtain the hash code of an object. Hash codes should not be persisted (i.e. in a database or file) as they are allowed to change from run to run.

Example:

Example 1

In some cases, System.Object.GetHashCode is implemented to simply return an integer value. The following example illustrates an implementation of System.Int32.GetHashCode , which returns an integer value:

using System;
public struct Int32 {
 int value;
 //other methods...

 public override int GetHashCode() {
 return value;
 }
}

Example 2

Frequently, a type has multiple data members that can participate in generating the hash code. One way to generate a hash code is to combine these fields using an xor (exclusive or) operation, as shown in the following example:

using System;
public struct Point {
 int x;
 int y; 
 //other methods
 
 public override int GetHashCode() {
 return x ^ y;
 }
}

Example 3

The following example illustrates another case where the type's fields are combined using xor (exclusive or) to generate the hash code. Notice that in this example, the fields represent user-defined types, each of which implements System.Object.GetHashCode (and should implement System.Object.Equals(System.Object) as well):

using System;
public class SomeType {
 public override int GetHashCode() {
 return 0;
 }
}

public class AnotherType {
 public override int GetHashCode() {
 return 1;
 }
}

public class LastType {
 public override int GetHashCode() {
 return 2;
 }
}
public class MyClass {
 SomeType a = new SomeType();
 AnotherType b = new AnotherType();
 LastType c = new LastType();

 public override int GetHashCode () {
 return a.GetHashCode() ^ b.GetHashCode() ^ c.GetHashCode();
 }
}

Avoid implementing System.Object.GetHashCode in a manner that results in circular references. In other words, if AClass.GetHashCode calls BClass.GetHashCode, it should not be the case that BClass.GetHashCode calls AClass.GetHashCode.

Example 4

In some cases, the data member of the class in which you are implementing System.Object.GetHashCode is bigger than a System.Int32. In such cases, you could combine the high order bits of the value with the low order bits using an XOR operation, as shown in the following example:

using System;
public struct Int64 {
 long value;
 //other methods...

 public override int GetHashCode() {
 return ((int)value ^ (int)(value >> 32));
 }
}

public System.Type GetType()

Gets the type of the current instance.

Returns: The instance of System.Type that represents the run-time type (the exact type) of the current instance.

For two objects x and y that have identical run-time types, System.Object.ReferenceEquals(System.Object,System.Object)(x.GetType(),y.GetType()) returns true .

Example:

The following example demonstrates the fact that System.Object.GetType returns the run-time type of the current instance:

using System;
public class MyBaseClass: Object {
}
public class MyDerivedClass: MyBaseClass {
}
public class Test {
   public static void Main() {
   MyBaseClass myBase = new MyBaseClass();
   MyDerivedClass myDerived = new MyDerivedClass();

   object o = myDerived;
   MyBaseClass b = myDerived;

   Console.WriteLine("mybase: Type is {0}", myBase.GetType());
   Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
   Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
   Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());
   }
}

The output is

mybase: Type is MyBaseClass

myDerived: Type is MyDerivedClass

object o = myDerived: Type is MyDerivedClass

MyBaseClass b = myDerived: Type is MyDerivedClass

protected System.Object MemberwiseClone()

Creates a shallow copy of the current instance.

Returns: A shallow copy of the current instance. The run-time type (the exact type) of the returned object is the same as the run-time type of the object that was copied.

System.Object.MemberwiseClone creates a new instance of the same type as the current instance and then copies each of the object's non-static fields in a manner that depends on whether the field is a value type or a reference type. If the field is a value type, a bit-by-bit copy of all the field's bits is performed. If the field is a reference type, only the reference is copied. The algorithm for performing a shallow copy is as follows (in pseudo-code): for each instance field f in this instance if (f is a value type) bitwise copy the field if (f is a reference type) copy the reference end for loop This mechanism is referred to as a shallow copy because it copies rather than clones the non-static fields. Because System.Object.MemberwiseClone implements the above algorithm, for any object, a, the following statements are required to be true: System.Object.MemberwiseClone does not call any of the type's constructors. If System.Object.Equals(System.Object) has been overridden, a.MemberwiseClone().Equals(a) might return false . For an alternate copying mechanism, see System.ICloneable . System.Object.MemberwiseClone is protected (rather than public) to ensure that from verifiable code it is only possible to clone objects of the same class as the one performing the operation (or one of its subclasses). Although cloning an object does not directly open security holes, it does allow an object to be created without running any of its constructors. Since these constructors may establish important invariants, objects created by cloning may not have these invariants established, and this may lead to incorrect program behavior. For example, a constructor might add the new object to a linked list of all objects of this class, and cloning the object would not add the new object to that list -- thus operations that relied on the list to locate all instances would fail to notice the cloned object. By making the method protected, only objects of the same class (or a subclass) can produce a clone and implementers of those classes are (presumably) aware of the appropriate invariants and can arrange for them to be true without necessarily calling a constructor.

Example:

The following example shows a class called MyClass as well as a representation of the instance of MyClass returned by System.Object.MemberwiseClone .

using System;
class MyBaseClass {
   public static string CompanyName = "My Company";
   public int age;
   public string name;
}

class MyDerivedClass: MyBaseClass {

   static void Main() {
   
   //Create an instance of MyDerivedClass
   //and assign values to its fields.
   MyDerivedClass m1 = new MyDerivedClass();
   m1.age = 42;
   m1.name = "Sam";

   //Do a shallow copy of m1
   //and assign it to m2.
   MyDerivedClass m2 = (MyDerivedClass) m1.MemberwiseClone();
   }
}

A graphical representation of m1 and m2 might look like this


+---------------+

|     42        |                           m1 

+---------------+

|     +---------|-----------------> "Sam" 

+---------------+                    /|\ 

                                      | 

+---------------+                     | 

|     42        |                     |      m2 

+---------------+                     | 

|      +--------|---------------------| 

+---------------+

public static System.Boolean ReferenceEquals(System.Object objA, System.Object objB)

Determines whether two object references are identical.

Parameter objA: First object to compare.

Parameter objB: Second object to compare.

Returns: True if a and b refer to the same object or are both null references; otherwise, false.

This static method provides a way to compare two objects for reference equality. It does not call any user-defined code, including overrides of System.Object.Equals(System.Object) .

Example:

using System;
class MyClass {
   static void Main() {
   object o = null;
   object p = null;
   object q = new Object();
   Console.WriteLine(Object.ReferenceEquals(o, p));
   p = q;
   Console.WriteLine(Object.ReferenceEquals(p, q));
   Console.WriteLine(Object.ReferenceEquals(o, p));
   }
}
   

The output is

True

True

False

public virtual System.String ToString()

Creates and returns a System.String representation of the current instance.

Returns: A System.String representation of the current instance.

System.Object.ToString returns a string whose content is intended to be understood by humans. Where the object contains culture-sensitive data, the string representation returned by System.Object.ToString takes into account the current system culture. For example, for an instance of the System.Double class whose value is zero, the implementation of System.Double.ToString might return "0.00" or "0,00" depending on the current UI culture. Although there are no exact requirements for the format of the returned string, it should as much as possible reflect the value of the object as perceived by the user. System.Object.ToString is equivalent to calling System.Object.GetType to obtain the System.Type object for the current instance and then returning the result of calling the System.Object.ToString implementation for that type. The value returned includes the full name of the type. It is recommended, but not required, that System.Object.ToString be overridden in a derived class to return values that are meaningful for that type. For example, the base data types, such as System.Int32, implement System.Object.ToString so that it returns the string form of the value the object represents. Subclasses that require more control over the formatting of strings than System.Object.ToString provides should implement System.IFormattable, whose System.Object.ToString method uses the culture of the current thread.

Example:

The following example outputs the textual description of the value of an object of type System.Object to the console.

using System;

class MyClass {
   static void Main() {
      object o = new object();
      Console.WriteLine (o.ToString());
   }
}
      

The output is

System.Object


Functions inherited from System.Collections.IList:

public System.Int32 Add(System.Object value)

Adds an item to the current instance.

Parameter value: The System.Object to add to the current instance.

Returns: A System.Int32 containing the index of the current instance into which the new element was inserted.

Throws: : The current instance is read-only or has a fixed size.

As described above. Use the System.Collections.IList.Add(System.Object) method to add another element to the current instance. The index into which that element is added is implementation-dependent.

public System.Void Clear()

Removes all items from the current instance.

Throws: : The current instance is read-only.

As described above. Implementations of this method can vary in how a call to this method affects the capacity of a list. Typically, the count is set to zero. The capacity can be set to zero, some default, or remain unchanged. Use this method to delete all values from the current instance.

public System.Boolean Contains(System.Object value)

Determines whether the current instance contains a specific value.

Parameter value: The System.Object to locate in the current instance.

Returns: true if the System.Object is found in the current instance; otherwise, false.

As described above. Use the System.Collections.IList.Contains(System.Object) method to determine if a particular System.Object is an element of the current instance.

public System.Int32 IndexOf(System.Object value)

Determines the index of a specific item in the current instance.

Parameter value: The System.Object to locate in the current instance.

Returns: The index of value if found in the current instance; otherwise, -1.

As described above. The default implementations of this method use System.Object.Equals(System.Object) to search for value in the current instance. Use System.Collections.IList.IndexOf(System.Object) to determine if a System.Object is contained in the current instance and, if it is contained, its index in the current instance.

public System.Void Insert(System.Int32 index, System.Object value)

Inserts an item to the current instance at the specified position.

Parameter index: A System.Int32 that specifies the zero-based index at which value is inserted.

Parameter value: The System.Object to insert into the current instance.

Throws: : index is not a valid index in the current instance (i.e. is greater than the number of elements in the current instance).

Throws: : The current instance is read-only or has a fixed size.

If index equals the number of items in the System.Collections.IList, then value is required to be appended to the end of the current instance. Use System.Collections.IList.Insert(System.Int32,System.Object) to place a new element into a specific position in the current instance.

public System.Void Remove(System.Object value)

Removes the first occurrence of a specified System.Object from the current instance.

Parameter value: The System.Object to remove from the current instance.

Throws: : The current instance is read-only or has a fixed size.

As described above. In addition, if value is null or is not found in the current instance, it is required that no exception be thrown and the current instance remain unchanged. The default implementations of this method use System.Object.Equals(System.Object) to search for value in the current instance. Use System.Collections.IList.Remove(System.Object) to delete a specified System.Object from the current instance.

public System.Void RemoveAt(System.Int32 index)

Removes the item at the specified index of the current instance.

Parameter index: A System.Int32 that specifies the zero-based index of the item to remove.

Throws: : index is not a valid index in current instance.

Throws: : The current instance is read-only or has a fixed size.

As described above. Use System.Collections.IList.RemoveAt(System.Int32) to delete a specified System.Object from the current instance.


Functions inherited from System.Collections.ICollection:

public System.Void CopyTo(System.Array array, System.Int32 index)

Copies the elements from the current instance to the specified System.Array, starting at the specified index in the array.

Parameter array: A one-dimensional, zero-based System.Array that is the destination of the elements copied from the current instance.

Parameter index: A System.Int32 that specifies the zero-based index in array at which copying begins.

Throws: : array is null.

Throws: : index < 0.

Throws: : array has more than one dimension. index is greater than or equal to array.Length. The sum of index and the System.Collections.ICollection.Count of the current instance is greater than array.Length.

Throws: : At least one element in the current instance is not assignment-compatible with the type of array.

As described above. Use this method to copy from a collection to a System.Array.


Functions inherited from System.Collections.IEnumerable:

public System.Collections.IEnumerator GetEnumerator()

Returns a System.Collections.IEnumerator that can be used for simple iteration over a collection.

Returns: A System.Collections.IEnumerator that can be used for simple iteration over a collection.

As described above. For a detailed description regarding the use of an enumerator, see System.Collections.IEnumerator.


Functions inherited from System.Collections.IEnumerable:

public System.Collections.IEnumerator GetEnumerator()

Returns a System.Collections.IEnumerator that can be used for simple iteration over a collection.

Returns: A System.Collections.IEnumerator that can be used for simple iteration over a collection.

As described above. For a detailed description regarding the use of an enumerator, see System.Collections.IEnumerator.


Functions inherited from System.Collections.ICollection:

public System.Void CopyTo(System.Array array, System.Int32 index)

Copies the elements from the current instance to the specified System.Array, starting at the specified index in the array.

Parameter array: A one-dimensional, zero-based System.Array that is the destination of the elements copied from the current instance.

Parameter index: A System.Int32 that specifies the zero-based index in array at which copying begins.

Throws: : array is null.

Throws: : index < 0.

Throws: : array has more than one dimension. index is greater than or equal to array.Length. The sum of index and the System.Collections.ICollection.Count of the current instance is greater than array.Length.

Throws: : At least one element in the current instance is not assignment-compatible with the type of array.

As described above. Use this method to copy from a collection to a System.Array.


Functions inherited from System.Collections.IEnumerable:

public System.Collections.IEnumerator GetEnumerator()

Returns a System.Collections.IEnumerator that can be used for simple iteration over a collection.

Returns: A System.Collections.IEnumerator that can be used for simple iteration over a collection.

As described above. For a detailed description regarding the use of an enumerator, see System.Collections.IEnumerator.


Functions inherited from System.Collections.IEnumerable:

public System.Collections.IEnumerator GetEnumerator()

Returns a System.Collections.IEnumerator that can be used for simple iteration over a collection.

Returns: A System.Collections.IEnumerator that can be used for simple iteration over a collection.

As described above. For a detailed description regarding the use of an enumerator, see System.Collections.IEnumerator.


Functions inherited from System.ICloneable:

public System.Object Clone()

Creates a copy of the current instance.

Returns: A System.Object of the same type as the current instance, containing copies of the non-static members of the current instance.

The exact behavior of this method is unspecified. The intent of the method is to provide a mechanism that constructs instances that are copies of the current instance, without regard for class-specific definitions of the term "copy". Use the System.Object.MemberwiseClone method to create a shallow copy of an object. For more information, see System.Object.MemberwiseClone . This method is required to return an instance of the same type as the current instance. Implement this method to provide class-specific copying behavior. Use the System.ICloneable.Clone method to obtain a copy of the current instance.

Example:

The following example shows an implementation of System.ICloneable.Clone that uses the System.Object.MemberwiseClone method to create a copy of the current instance.

using System;
class MyClass :ICloneable {
    public int myField;
    public MyClass() {
        myField = 0;
    }
    public MyClass(int value) {
        myField = value;
    }
    public object Clone() {
        return this.MemberwiseClone();
    }
}
public class TestMyClass {
    public static void Main() {
        MyClass my1 = new MyClass(44);
        MyClass my2 = (MyClass) my1.Clone();
        Console.WriteLine("my1 {0} my2 {1}",my1.myField, my2.myField);
        my2.myField = 22;
        Console.WriteLine("my1 {0} my2 {1}",my1.myField, my2.myField);
    }
}

The output is

my1 44 my2 44

my1 44 my2 22