[Serializable] |
This implementation does not provide a synchronized (thread-safe) wrapper for a StringCollection, but derived classes can create their own synchronized versions of the StringCollection using the StringCollection.SyncRoot property.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
String comparisons are case-sensitive.
Indexes in this collection are zero-based.
ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. |
Count | Read-only Gets the number of strings contained in the StringCollection. |
IsReadOnly | Read-only Gets a value indicating whether the StringCollection is read-only. |
IsSynchronized | Read-only Gets a value indicating whether access to the StringCollection is synchronized (thread-safe). |
Item | Read-write Gets or sets the element at the specified index. |
SyncRoot | Read-only Gets an object that can be used to synchronize access to the StringCollection. |
Add | Adds a string to the end of the StringCollection. |
AddRange | Copies the elements of a string array to the end of the StringCollection. |
Clear | Removes all the strings from the StringCollection. |
Contains | Determines whether the specified string is in the StringCollection. |
CopyTo | Copies the entire StringCollection values to a one-dimensional array of strings, starting at the specified index of the target array. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetEnumerator | Returns a StringEnumerator that can iterate through the StringCollection. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IndexOf | Searches for the specified string and returns the zero-based index of the first occurrence within the StringCollection. |
Insert | Inserts a string into the StringCollection at the specified index. |
Remove | Removes the first occurrence of a specific string from the StringCollection. |
RemoveAt | Removes the string at the specified index of the StringCollection. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
ICollection.CopyTo | Copies the entire StringCollection to a compatible one-dimensional Array, starting at the specified index of the target array. |
IEnumerable.GetEnumerator | Returns a IEnumerator that can iterate through the StringCollection. |
IList.Add | Adds an object to the end of the StringCollection. |
IList.Contains | Determines whether an element is in the StringCollection. |
IList.IndexOf | Searches for the specified Object and returns the zero-based index of the first occurrence within the entire StringCollection. |
IList.Insert | Inserts an element into the StringCollection at the specified index. |
IList.Remove | Removes the first occurrence of a specific object from the StringCollection. |
Hierarchy:
public StringCollection(); |
public int Count {get;}
|
public bool IsReadOnly {get;}
|
A collection that is read-only does not allow the addition, removal, or modification of elements after the collection is created.
A StringCollection instance is always writable.
public bool IsSynchronized {get;}
|
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the StringCollection.SyncRoot during the entire enumeration:
StringCollection myCollection = new StringCollection(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
public string this[int index] {get; set;}
|
index
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is less than zero. -or- index is equal to or greater than StringCollection.Count. |
myCollection[index]
.
public object SyncRoot {get;}
|
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the StringCollection.SyncRoot during the entire enumeration:
StringCollection myCollection = new StringCollection(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
value
public void AddRange( |
value
Exception Type | Condition |
---|---|
ArgumentNullException | value is null. |
The array itself can not be null but it can contain elements that are null.
public void Clear(); |
value
This method performs a linear search; therefore, the average execution time is proportional to StringCollection.Count. That is, this method is an O(n) operation, where n is StringCollection.Count.
This method determines equality by calling Object.Equals. String comparisons are case-sensitive.
array
index
Exception Type | Condition |
---|---|
ArgumentNullException | array is null. |
ArgumentOutOfRangeException | index is less than zero. |
ArgumentException | array is multidimensional. -or- index is equal to or greater than the length of array. -or- The number of elements in the source StringCollection is greater than the available space from index to the end of the destination array. |
InvalidCastException | The type of the source StringCollection cannot be cast automatically to the type of the destination array. |
The elements are copied to the Array in the same order in which the enumerator of the StringCollection iterates through the StringCollection.
~StringCollection(); |
public StringEnumerator GetEnumerator(); |
The enumerator does not have exclusive access to the collection.
When an enumerator is created, it takes a snapshot of the current state of the collection. If changes are made to the collection, such as adding, modifying or deleting elements, the snapshot gets out of sync and the enumerator throws an InvalidOperationException. Two enumerators created from the same collection at the same time can have different snapshots of the collection.
The enumerator is in an invalid state if it is positioned before the first element in the collection or after the last element in the collection. Whenever the enumerator is in an invalid state, calling IEnumerator.Current throws an exception.
Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. Therefore, after an enumerator is created or after a IEnumerator.Reset, IEnumerator.MoveNext must be called to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.
IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called.
After the end of the collection is passed, the enumerator is again in an invalid state and calling IEnumerator.MoveNext returns false. Calling IEnumerator.Current throws an exception if the last call to IEnumerator.MoveNext returned false.
public virtual int GetHashCode(); |
public Type GetType(); |
value
This method determines equality by calling Object.Equals. String comparisons are case-sensitive.
index
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is less than zero. -or- index greater than StringCollection.Count. |
If index is equal to StringCollection.Count, value is added to the end of StringCollection.
In collections of contiguous elements, such as lists, the elements that follow the insertion point move down to accomodate the new element. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.
protected object MemberwiseClone(); |
public void Remove( |
value
RemoveAt(IndexOf(value))
repeatedly while StringCollection.IndexOf does not return -1.This method performs a linear search; therefore, the average execution time is proportional to StringCollection.Count. That is, this method is an O(n) operation, where n is StringCollection.Count.
This method determines equality by calling Object.Equals. String comparisons are case-sensitive.
In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.
public void RemoveAt( |
index
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is less than zero. -or- index is equal to or greater than StringCollection.Count. |
array
index
Exception Type | Condition |
---|---|
ArgumentNullException | array is null. |
ArgumentOutOfRangeException | index is less than zero. |
ArgumentException | array is multidimensional. -or- index is equal to or greater than the length of array. -or- The number of elements in the source StringCollection is greater than the available space from index to the end of the destination array. |
InvalidCastException | The type of the source StringCollection cannot be cast automatically to the type of the destination array. |
This method uses Array.Copy to copy the elements.
IEnumerator IEnumerable.GetEnumerator(); |
The enumerator does not have exclusive access to the collection.
When an enumerator is created, it takes a snapshot of the current state of the collection. If changes are made to the collection, such as adding, modifying or deleting elements, the snapshot gets out of sync and the enumerator throws an InvalidOperationException. Two enumerators created from the same collection at the same time can have different snapshots of the collection.
The enumerator is in an invalid state if it is positioned before the first element in the collection or after the last element in the collection. Whenever the enumerator is in an invalid state, calling IEnumerator.Current throws an exception.
Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. Therefore, after an enumerator is created or after a IEnumerator.Reset, IEnumerator.MoveNext must be called to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.
IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called.
After the end of the collection is passed, the enumerator is again in an invalid state and calling IEnumerator.MoveNext returns false. Calling IEnumerator.Current throws an exception if the last call to IEnumerator.MoveNext returned false.
value
Exception Type | Condition |
---|---|
NotSupportedException | The StringCollection is read-only. -or- The StringCollection has a fixed size. |
If StringCollection.Count is less than the capacity, this method is an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is StringCollection.Count.
value
This method determines equality by calling Object.Equals.
value
This method performs a linear search. On average, this is an O(n /2) operation, where n is StringCollection.Count. The longest search is an O(n) operation.
This method determines equality by calling Object.Equals.
index
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is less than zero. -or- index is greater than StringCollection.Count. |
NotSupportedException | The StringCollection is read-only. -or- The StringCollection has a fixed size. |
If index is equal to StringCollection.Count, value is added to the end of StringCollection.
In collections of contiguous elements, such as lists, the elements that follow the insertion point move down to accomodate the new element. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.
void IList.Remove( |
value
Exception Type | Condition |
---|---|
NotSupportedException | The StringCollection is read-only. -or- The StringCollection has a fixed size. |
This method determines equality by calling Object.Equals.
In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.
public virtual string ToString(); |