Collection<T> Class provides the base class for a generic collection. Here T is the type of elements in the collection. This class comes under the System.Collections.ObjectModel namespace.
Characteristics:
- The Collection<T> class can be used immediately by creating an instance of one of its constructed types.
- The Collection<T> class provides protected methods that can be used to customize its behavior when adding and removing items, clearing the collection, or setting the value of an existing item.
- Most Collection<T> objects can be modified. However, a Collection object that is initialized with a read-only IList<T> object cannot be modified.
- Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
- Collection<T> accepts null as a valid value for reference types and allows duplicate elements.
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 >();
myColl.Add(2);
myColl.Add(3);
myColl.Add(4);
myColl.Add(5);
foreach ( int i in myColl)
{
Console.WriteLine(i);
}
}
}
|
Output:
2
3
4
5
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()
{
Collection< string > myColl = new Collection< string >();
myColl.Add( "A" );
myColl.Add( "B" );
myColl.Add( "C" );
myColl.Add( "D" );
myColl.Add( "E" );
Console.WriteLine( "Count : " + myColl.Count);
Console.WriteLine( "Element at index 2 is : " + myColl[2]);
Console.WriteLine( "Element at index 3 is : " + myColl[3]);
}
}
|
Output:
Count : 5
Element at index 2 is : C
Element at index 3 is : D
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 entire Collection<T> to a compatible one-dimensional Array, starting at the specified index of the target array. |
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 specified object and returns the zero-based index of the first occurrence within the entire Collection<T>. |
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:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
class GFG {
public static void Main()
{
Collection< string > myColl = new Collection< string >();
myColl.Add( "A" );
myColl.Add( "B" );
myColl.Add( "C" );
myColl.Add( "D" );
myColl.Add( "E" );
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()
{
Collection< string > myColl = new Collection< string >();
myColl.Add( "A" );
myColl.Add( "B" );
myColl.Add( "C" );
myColl.Add( "D" );
myColl.Add( "E" );
string [] myArr = new string [myColl.Count];
myColl.CopyTo(myArr, 0);
foreach ( string str in myArr)
{
Console.WriteLine(str);
}
}
}
|
Output:
A
B
C
D
E
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!