Working with Collection<T> in C# Language


In this tutorial we will show you how to use Collection class in C#. Collection class supports some advantages when dealing with group of object, especially when you want to modify and create your own Collection class.

Let's go

In C# language Collection class defines some virtual/abstract members and enables you to override these members to modify their behaviors. You can declare an instance of Collection class and specify the type of elements in this collection, then use some default built-in members of this class, or you can derive from it and create your own Collection class, with index access like an array. Here is the list of some common members of Collection class:

Count Gets the number of elements actually contained in the collection
AddAdds an object to the end of the collection
ClearRemoves all elements from the list
ContainsDetermines whether an element is in the collection

Bellow is an example of how to use Collections in C#:

  1. using System;
  2. using System.Text;
  3. using System.Collections.ObjectModel;//this namespace must be insert here when you want to use Collection class
  5. namespace MyFirstProgram
  6. {
  7. class CollectionTest
  8. {
  9. static void Main(string[] args)
  10. {
  11. CollectionTest colObj = new CollectionTest();
  12. colObj.TestCollection();
  14. Console.ReadKey();
  15. }
  17. private void TestCollection()
  18. {
  19. //declare an collection object which has int type.
  20. Collection<int> my_collection = new Collection<int>();
  21. //add elements to collection with Add memeber
  22. for (int i = 0; i < 10; i++)
  23. {
  24. my_collection.Add(i);
  25. }
  26. //print number of elements in collection with Count member
  27. Console.WriteLine("Length of collection " + my_collection.Count);
  28. }
  29. }
  30. }

In above code we declare a collection of type Int and then add elements to our collection, then print the number of elements in this collection. Everything is simple and easy.

Deriving your own collection

In case of making your own collection, you can derive from Collection class. Collection defines some members that you can override as follows:

ClearItems Removes all elements from the collection. Can change the behavior of the Clear method.
InsertItemInserts an element into the collection at the specified index.
RemoveItemRemoves the element at the specified index
SetItemReplaces the element at the specified index.

Example bellow demonstrate how you can derive and make your own class:

  1. public class IntCollection : Collection<int>
  2. {
  3. protected override void InsertItem(int index, int item)
  4. {
  5. Console.WriteLine("Insert new item {0} at position {1}", item, index);
  6. Base.InsertItem(index, item);
  7. }
  9. static void Main(string[] args)
  10. {
  11. IntCollection colObj = new IntCollection();
  12. colObj.InsertItem(0, 1);
  14. Console.WriteLine("Length of collection " + colObj.Count);
  15. Console.ReadKey();
  16. }
  17. }

Here we declare an IntCollection class that derives from Collection, and then we override the member InsertItem and write code to modify as you want.


This tutorial has introduced you how to use Collection to manipulate group of objects, with similar to other types of classes used for manipulating with group of objects. With Collection you can also derived to write your own collection class to modify the behavior of some virtual members.


this web site is really very helpful for me and all who learning their programming lan.

Add new comment