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:
Collection<string> fruits = new Collection<string>();
Characteristics
- Belongs to the System.Collections.ObjectModel namespace.
- Stores elements of a specific type (T).
- Provides methods to add, remove and search elements.
- Allows index-based access like arrays and List<T>.
- Designed for scenarios where you want to customize collection behavior by overriding methods.
Constructors
| 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:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
class GFG {
public static void Main(){
Collection<int> myColl = new Collection<int>();
// Adding elements in Collection myColl
myColl.Add(2);
myColl.Add(3);
myColl.Add(4);
myColl.Add(5);
// Displaying the elements in myColl
foreach(int i in myColl){
Console.WriteLine(i);
}
}
}
Output
2 3 4 5
Collection Properties
| 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:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
class GFG {
public static void Main()
{
// Creating a collection of strings
Collection<string> myColl = new Collection<string>();
// Adding elements in Collection myColl
myColl.Add("A");
myColl.Add("B");
myColl.Add("C");
myColl.Add("D");
myColl.Add("E");
// To print the count of elements in Collection
Console.WriteLine("Count : " + myColl.Count);
// Get the element at index 2
Console.WriteLine("Element at index 2 is : " + myColl[2]);
// Get the element at index 3
Console.WriteLine("Element at index 3 is : " + myColl[3]);
}
}
Output
Count : 5 Element at index 2 is : C Element at index 3 is : D
Collection Methods
| 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:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
class GFG{
public static void Main(){
// Creating a collection of strings
Collection<string> myColl = new Collection<string>();
myColl.Add("A");
myColl.Add("B");
myColl.Add("C");
myColl.Add("D");
myColl.Add("E");
// Check if an element exists in the Collection (returns true/false)
Console.WriteLine(myColl.Contains("A"));
}
}
Output
True
Example 2:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
class GFG{
public static void Main(){
// Creating a collection of strings
Collection<string> myColl = new Collection<string>();
myColl.Add("A");
myColl.Add("B");
myColl.Add("C");
myColl.Add("D");
myColl.Add("E");
// Creating a string array
string[] myArr = new string[myColl.Count];
myColl.CopyTo(myArr, 0);
foreach(string str in myArr){
Console.WriteLine(str);
}
}
}
Output
A B C D E