Understanding Collections and Collections Interfaces

Understanding Collections and Collections Interfaces

13 Mar 2024
7 min read
Learn via Video Course & by Doing Hands-on Labs

C# Programming For Beginners Free Course

Collections in C#: An Overview

A collection is a set of related objects. Unlike arrays, a collection can grow and shrink dynamically as the number of objects added or deleted. A collection is a class, so you must declare a new collection before you can add elements to that collection. In this C# Tutorial, we will explore collections-and-collections-interfaces which will include What are collections in c#, What are collections interfaces in c#, and What types of collections interfaces are in c#.

What is Collections?

The .NET Framework provides various collections like ArrayList, HashTable, SortedList, Stack, and Queue, etc. All these collections exist in the System. Collections namespace.

Represents an array of objects whose size is dynamically increased or decreased as required. It is an alternative to an array. It supports add and remove methods for adding and removing objects from the collection.
Represents a collection of objects which are stored in key/value pair fashion, where the key is a hash code and the value is an object. The key is used to access or manipulate the objects in the collection. It supports add and remove methods for adding and removing objects from the collection.
Represents a collection of objects that are stored in key/value pairs fashion like HashTable and can be sorted by the keys. It can be accessible by key or by index number. Typically, it is a combination of ArrayList and HashTable. It supports add and remove methods for adding and removing objects from the collection.
Represents a last in, first out (LIFO) collection of objects. It supports push-and-pop methods for adding and removing objects from the collection.
Represents a first in, first out (FIFO) collection of objects. It supports Enqueue and Dequeue methods for adding and removing objects from the collection.

Read More - C Sharp Interview Questions

Collections in C#:

Example of Collections:

Let's elaborate collections in C# Compiler with its example
 using System; 
using System.Collections; 

class Scholarhat { 
	public static void Main() 
		Queue CourseQueue = new  Queue(); 


		Console.Write("Total number of courses present in the Queue are: "); 


		// Displaying the beginning element of Queue 
		Console.WriteLine("Course: " + CourseQueue.Peek()); 


 Total number of courses present in the Queue are: 5
Course: C#


In the above program, we illustrate nongeneric collections using a queue, first, we created a queue named "CourseQueue".Then We Inserted the elements into the Queue Then we tried to display the count of elements along with the starting course.

What are Collection Interfaces?

All of the collection types use some common interfaces. These common interfaces define the basic functionality for each collection class. The key collections interfaces are – IEnumerable, ICollection, IDictionary, and IList.

Collection Interfaces

IEnumerable acts as a base interface for all the collection types that is extended by ICollection. ICollection is further extended by IDictionary and IList.

Provides an enumerator that supports a simple iteration over a non-generic collection.
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Represents a nongeneric collection of key/value pairs.
Represents a non-generic collection of objects that can be individually accessed by index.

All collections interfaces are not implemented by all the collections. It depends on the collection's nature.

The IEnumerable Interface: Syntax

 public IEnumerator GetEnumerator(  )
    return (IEnumerator) new ListBoxEnumerator(this);
  • Here The Enumerator must implement the IEnumerator methods and properties.
  • These can be implemented either directly by the container class or by a separate class.
  • The latter approach is preferred because it encapsulates this responsibility in the Enumerator class.

The ICollectionInterface: Syntax

 ICollection names = new List();
Console.WriteLine(names.Count); // Outputs the count of items

The IDictionary: Syntax

 IDictionary ages = new();
ages["Pragati"] = 18;
ages.Add("surbhi", 17);
Console.WriteLine(ages["Pragati"]); // Access value by key

The IList: Syntax

 IList numbers = new List { 1, 2, 3,4,5 };
numbers[0] = 10; // Access and modify elements by index
Console.WriteLine(numbers.IndexOf(3)); // Outputs the index of '5'
I hope you will enjoy the collections and collections interfaces while programming with C#. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome. Also, Consider our C# Programming Course for a better understanding of C# concepts.


Q1. What is the difference between collection and collection interface?

A Collection represents a group of objects known as its elements. The Collection interface is used to pass around collections of objects where maximum generality is desired.

Q2. What is the collections interface?

It is used to pass around collections of objects where maximum generality is desired.

Q3. What are the collections in C#?

It made to more effectively organize, store, and modify comparable data. 

Take our free csharp skill challenge to evaluate your skill

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.


Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A
  • 10+ Real-world Projects
  • Career Coaching
  • Email Support
Upto 66% OFF

To get full access to all courses

Accept cookies & close this