13
SepUnderstanding Collections and Collections Interfaces
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.
Read More - C Sharp Interview Questions
Example of Collections:
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.
IEnumerable acts as a base interface for all the collection types that is extended by ICollection. ICollection is further extended by IDictionary and IList.
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?
Q2. What is the collections interface?
Q3. What are the collections in C#?
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.