C# Collections

Our next tutorial chapter is C# collections, which is further divided into 3 parts: Generic Collection, Non-Generic Collections and Concurrent, before we procede to look into each of collection type, let's understand what is a collection.

What is Collection in C#?

Collection are specialized classes for data storage and retrieval, these classes provide support for stacks, queues, lists and hash tables.

  • Collections are objects that allows you to group other objects
  • Most of collection implement the same interface - ICollection, which introduces very basic members of each collection
  • Collections are always of reference types
  • Enumerable types, where elements might usually be accessed by index or key.

Types of collections:

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

System.Collections.Generic Namespace

The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.

generic-collection-csharp-min.png

The System.Collections.Generic namespace has following classes:

  • List
  • Stack
  • Queue
  • LinkedList
  • HashSet
  • Dictionary

System.Collections Namespace

The namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.These classes are legacy. It is suggested now to use System.Collections.Generic classes. The System.Collections namespace has following classes:

  • ArrayList
  • Stack
  • Queue
  • Hashtable

System.Collections.Concurrent Namespace

The System.Collections.Concurrent namespace provides several thread-safe collection classes that should be used in place of the corresponding types in the and namespaces whenever multiple threads are accessing the collection concurrently. However, access to elements of a collection object through extension methods or through explicit interface implementations are not guaranteed to be thread-safe and may need to be synchronized by the caller.

The System.Collections.Concurrent namespace has following classes:

  • BlockingCollection
  • ConcurrentBag
  • ConcurrentStack
  • ConcurrentQueue
  • ConcurrentDictionary
  • Partitioner
  • Partitioner
  • OrderablePartitioner

Share Tweet