Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Collections in C#

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Data can be stored as objects in arrays or collections. While arrays only allow a fixed number of objects to be stored, Collections are far more flexible. They can shrink and grow as elements are added and deleted from it. It contains a set of classes that contain elements in an organised manner. They provide support for stacks, queues, lists, and hash tables.

Collections

A collection is a class whose instance must be declared before adding elements to that collection. Collections help perform various data manipulation operations like insertion, deletion, sorting, searching, etc. Collections in C# are composed of many classes. The most common ones are

  • System.Collections.Generic
     
  • System.Collections
     
  • System.Collections.Concurrent

System.Collections.Generic

Generic Collections work on a specific data type. It is used when we want to build a collection of elements of a single data type. This enforces type safety so that no other data type can be added randomly. Hence, we do not have to determine an element’s data type or convert it during retrieval.

The following table shows some of the frequently used classes of the System.Collections.Generic namespace.

Example:

using System;
using System.Collections.Generic;

class NewList
{
    public static void Main(String[] args)
    {
       // Creating a List of integers
       List<int> l = new List<int>();

       // adding items
       for (int j = 1; j <=5; j++) 
       {
         l.Add(j);
       }

       // Displaying list elements
       foreach(int items in l)
       {
         Console.WriteLine(items);
       }
   }
}

 

Output:

1
2
3
4
5

System.Collections

System.Collections stores elements as objects instead of a particular data type. It is a general-purpose data structure that works on any type of object but is not safe. Hence, it is recommended to use Generic collections instead of non-generic ones.

Some common classes of the System.Collections namespace are

Example:

using System;
using System.Collections;

class NewArraylist
{
    public static void Main()
    {
       // Creating an ArrayList
       ArrayList l = new ArrayList();

       // Adding elements 
       l.Add("Hello");
       l.Add("World");

       Console.WriteLine("Count : " + l.Count);

       foreach(string i in l)
       {
         Console.WriteLine(i);
       }
   }
}

 

Output:

Count : 2
Hello
World

System.Collections.Concurrent

It provides various threads-safe collection classes that are used when multiple threads access a collection simultaneously. Some classes of the System.Collections.Concurrent are

 

Also check out this article - singleton design pattern in c#

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

  1. What are the collections used to create key-value pairs?

    Dictionary, SortedList and Hashtable can be used to create key-value pairs. Dictionary is a generic class. SortedList is a generic class that sorts its elements in a specified order. Hashtable is very much similar to Dictionary but is non-generic.

     
  2. What is the difference between generic and non-generic stack/queue?

    Non-Generic stacks and queues are defined under System.Collections namespace. A Generic stack/queue can only store similar types of elements. A Non-Generic stack/Queue can store elements of similar or different types.

Key Takeaways

This article extensively discusses Collections in C#. We hope that this blog has helped you enhance your knowledge about the different kinds of Collections in C#. If you would like to learn more, check out our articles on the Coding Ninjas Library. Do upvote our blog to help other ninjas grow. Happy Coding!

Check out this problem - Queue Implementation

Related Links:

Interfaces in C#

Dictionary in C#

Topics covered
1.
Introduction
2.
Collections
2.1.
System.Collections.Generic
2.2.
System.Collections
2.3.
System.Collections.Concurrent
3.
FAQs
4.
Key Takeaways