VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/collection-class-c-sharp/

⇱ Collection Class in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Collection Class in C#

Last Updated : 10 Sep, 2025

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:

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

ConstructorDescription
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:


Output
2
3
4
5

Collection Properties

PropertyDescription
CountGets the number of elements actually contained in the Collection<T>.
ItemsGets a IList<T> wrapper around the Collection<T>.
Item[Int32]Gets or sets the element at the specified index.

Example:


Output
Count : 5
Element at index 2 is : C
Element at index 3 is : D

Collection Methods

MethodDescription
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:


Output
True

Example 2:


Output
A
B
C
D
E
Comment

Explore