![]() |
VOOZH | about |
The Collection<T> class is a generic collection in C# provided by the .NET framework under the System.Collections.ObjectModel namespace. It represents a dynamic, strongly typed list of objects that can be accessed by index (like a List<T>), but it is designed to be more extensible.
Syntax:
| Constructor | Description |
|---|---|
| Collection<T>() | Initializes a new instance of the Collection<T> class that is empty. |
| Collection<T>(IList<T>) | Initializes a new instance of the Collection<T> class as a wrapper for the specified list. |
Example:
2 3 4 5
| Property | Description |
|---|---|
| Count | Gets the number of elements actually contained in the Collection<T>. |
| Items | Gets a IList<T> wrapper around the Collection<T>. |
| Item[Int32] | Gets or sets the element at the specified index. |
Example:
Count : 5 Element at index 2 is : C Element at index 3 is : D
| Method | Description |
|---|---|
| Add(T) | Adds an object to the end of the Collection<T>. |
| Clear() | Removes all elements from the Collection<T>. |
| ClearItems() | Removes all elements from the Collection<T>. |
| Contains(T) | Determines whether an element is in the Collection<T>. |
| CopyTo(T[], Int32) | Copies the elements of the collection to an array, starting at a specified index. |
| Equals(Object) | Determines whether the specified object is equal to the current object. |
| GetEnumerator() | Returns an enumerator that iterates through the Collection<T>. |
| GetHashCode() | Serves as the default hash function. |
| GetType() | Gets the Type of the current instance. |
| IndexOf(T) | Searches for the object and returns the zero-based index of the first occurrence. |
| Insert(Int32, T) | Inserts an element into the Collection<T> at the specified index. |
| InsertItem(Int32, T) | Inserts an element into the Collection at the specified index. |
| MemberwiseClone() | Creates a shallow copy of the current Object. |
| Remove(T) | Removes the first occurrence of a specific object from the Collection<T>. |
| RemoveAt(Int32) | Removes the element at the specified index of the Collection<T>. |
| RemoveItem(Int32) | Removes the element at the specified index of the Collection<T>. |
| SetItem(Int32, T) | Replaces the element at the specified index. |
| ToString() | Returns a string that represents the current object. |
Example 1:
True
Example 2:
A B C D E