Theme-Logo
  •   .Net
    •   C Sharp(C#)
    •   Web API
    •   Micro Services
    •   ASP.Net
    • ASP.Net MVC
    • .Net Core
  •   Database
    • SQL Server
    • Oracle
    • PostgreSQL
  •   jQuery
    • jQuery Tutorials
    • jQuery Plugins
    • jQuery UI
    • More on jquery
  •   Tutorials
    • Microservices Tutorials
    • DotNet Core Tutorials
    • PostgreSql Tutorials

Collections in C Sharp (C#)

In c#, the collection is a class that is useful to manage a group of objects in a flexible manner to perform various operations like insert, update, delete, get, etc. on the object items in a dynamic manner based on our requirements. Generally, while working in c# applications we will get a requirement like to create or manage a group of related objects. In that case, we have two ways to create group objects in c# i.e. by using the arrays and collections. We can use arrays in c# but those are useful only when we are working with a fixed number of strongly-typed objects.

So, to solve this problem Microsoft has introduced Collections in c# to work with a group of objects which can grow or shrink dynamically based on our requirements. In c#, the collection is a class so we just need to declare an instance of the class before we perform any operations like add, delete, etc. on the defined collection and the collections are implemented by using the IEnumerable interface so we can access collection items by using a foreach loop.

C# Collection Types

In c#, we have a different type of collection classes are available, those are

  • Non-Generic (System.Collections)
  • Generic (System.Collections.Generic)
  • Concurrent (System.Collections.Concurrent : Available from .NET Framework 4 )
  • C# Non-Generic Collections

    In c#, non-generic collection classes are useful to store elements of different data types and these are provided by System.Collections namespace. Now, these collection classes are legacy types so whenever possible try to use generic collections (System.Collections.Generic) or concurrent collections (System.Collections.Concurrent). Following are the different type of non-generic collection classes which are provided by System.Collections namespace.

  • ArrayList : It is useful to represent an array of objects whose size is dynamically increased as required.
  • Queue : It is useful to represent a FIFO (First in, First Out) collection of objects.
  • Stack : It is useful to represent a LIFO (Last in, First Out) collection of objects.
  • Hashtable : It is useful to represent a collection of key/value pairs.
  • C# Generic Collections

    In c#, generic collections will enforce a type safety so we can store only the elements which are having the same data type and these are provided by System.Collections.Generic namespace. Following are the different type of generic collection classes which are provided by System.Collections.Generic namespace.

  • List : It is useful to represent an array of objects whose size is dynamically increased as required.
  • Queue : It is useful to represent a FIFO (First in, First Out) collection of objects.
  • Stack : It is useful to represent a LIFO (Last in, First Out) collection of objects.
  • SortedList<K,V> :It is useful to represent a collection of key/value pairs that are sorted by a key.
  • Dictionary<K,V> :It is useful to represent a collection of key/value pairs that are organized based on the key.
  • C# Concurrent Collections

    In c#, concurrent collections are useful to access collection items from multiple threads and these are available from .NET Framework 4 with System.Collections.Concurrent namespace. In case, if we are using multiple threads to access a collection concurrently, then we need to use concurrent collections instead of non-generic and generic collections. Following are the different type of concurrent collection classes which are provided by System.Collections.Concurrent namespace.

  • BlockingCollection : It is useful to represent an array of objects whose size is dynamically increased as required.
  • ConcurrentBag: It is useful to represent a thread-safe, unordered collection of objects.
  • ConcurrentDictionary <K,V>:It is useful to represent a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.
  • ConcurrentQueue: It is useful to represent a thread-safe FIFO (First in, First Out) collection.
  • ConcurrentStack:It is useful to represent a thread-safe LIFO (Last in, First Out) collection.
  • Trending Post
    What is Anonymous methods in C#.
    Why Is It A Bad Idea To Throw Your Own Exceptions.
    How's The DLL Hell Problem Solved In .net?
    Interview Questions and Answers
    Describe the Async method of C#?
    Is it possible to serialise hashtables?
    Explain the differences between “out” and “ref” parameters in C#?
    How would you implement the Singleton design pattern in C#?
    What Are The Ways To Deploy An Assembly?
    What Are Advantages And Disadvantages Of Microsoft-provided Data Provider Classes In Ado.net?
    About us

    DotNet Palace is a community platform created and maintained by The articles are mainly focused on Microsoft stack technologies like C#, ASP.Net, MVC, .Net Core, SQL Server and Oracle, PostgreSQL, SQLite etc. To improve the site's content you can send your valuable suggestions at info.dotnetpalace@gmail.com

    Quick links
  • SOLID Principles
  • Questions
  • OOPS Principles
  • DotNet Palace Tutorials
  • Privacy Policy
  • Terms and Condition