Understanding Collections and Collections Interfaces

Understanding Collections and Collections Interfaces

23 May 2024
Advanced
128K Views
7 min read
Learn via Video Course & by Doing Hands-on Labs

Free C# Course Online

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.

Class
Description
ArrayList
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.
Hashtable
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.
SortedList
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.
Stack
Represents a last in, first out (LIFO) collection of objects. It supports push-and-pop methods for adding and removing objects from the collection.
Queue
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(); 

		CourseQueue.Enqueue("C#"); 
		CourseQueue.Enqueue("Java"); 
		CourseQueue.Enqueue("SQL"); 
		CourseQueue.Enqueue("Python"); 
		CourseQueue.Enqueue("C"); 

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

		Console.WriteLine(CourseQueue.Count); 

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

Output

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

Explanation

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.

Interface
Description
IEnumerable
Provides an enumerator that supports a simple iteration over a non-generic collection.
ICollection
Defines size, enumerators, and synchronization methods for all nongeneric collections.
IDictionary
Represents a nongeneric collection of key/value pairs.
IList
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();
names.Add("Pragati");
names.Remove("surbhi");
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'
Conclusion:
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.

FAQs

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 Csharp skill challenge to evaluate yourself!

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

GET FREE CHALLENGE

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Generative AI For Software Developers Jul 20 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Angular Certification Course Jul 20 SAT, SUN
Filling Fast
06:00PM to 08:00PM (IST)
Get Details
Azure Master Class Jul 20 SAT, SUN
Filling Fast
03:00PM to 05:00PM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Azure Developer Certification Training Jul 28 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Data Structures and Algorithms Training with C# Jul 28 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Angular Certification Course Aug 11 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

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.
Accept cookies & close this