Collections in C# - GeeksforGeeks

22 Apr, 2020
Collections standardize the way of which the objects are handled by your program. In other words, it contains a set of classes to contain elements in a generalized manner. With the help of collections, the user can perform several operations on objects like the store, update, delete, retrieve, search, sort etc.
C# divide collection in several classes, some of the common classes are shown below:
Generic collection in C# is defined in System.Collection.Generic namespace. It provides a generic implementation of standard data structure like linked lists, stacks, queues, and dictionaries. These collections are type-safe because they are generic means only those items that are type-compatible with the type of the collection can be stored in a generic collection, it eliminates accidental type mismatches. Generic collections are defined by the set of interfaces and classes. Below table contains the frequently used classes of the System.Collections.Generic namespace:
Example:
// C# program to illustrate the concept // of generic collection using List<T>using System;using System.Collections.Generic;  class Geeks {      // Main Method    public static void Main(String[] args)    {          // Creating a List of integers        List<int> mylist = new List<int>();          // adding items in mylist        for (int j = 5; j < 10; j++) {            mylist.Add(j * 3);        }          // Displaying items of mylist        // by using foreach loop        foreach(int items in mylist)        {            Console.WriteLine(items);        }    }}
15
18
21
24
27

Non-Generic collection in C# is defined in System.Collections namespace. It is a general-purpose data structure that works on object references, so it can handle any type of object, but not in a safe-type manner. Non-generic collections are defined by the set of interfaces and classes. Below table contains the frequently used classes of the System.Collections namespace:
Example:
// C# to illustrate the concept// of non-generic collection using Queueusing System;using System.Collections;  class GFG {      // Driver code    public static void Main()    {          // Creating a Queue        Queue myQueue = new Queue();          // Inserting the elements into the Queue        myQueue.Enqueue("C#");        myQueue.Enqueue("PHP");        myQueue.Enqueue("Perl");        myQueue.Enqueue("Java");        myQueue.Enqueue("C");          // Displaying the count of elements        // contained in the Queue        Console.Write("Total number of elements present in the Queue are: ");          Console.WriteLine(myQueue.Count);          // Displaying the beginning element of Queue        Console.WriteLine("Beginning Item is: " + myQueue.Peek());    }}
Total number of elements present in the Queue are: 5
Beginning Item is: C#

Note: C# also provides some specialized collection that is optimized to work on a specific type of data type and the specialized collection are found in System.Collections.Specialized namespace.
It came in .NET Framework Version 4 and onwards. It provides various threads-safe collection classes that are used in the place of the corresponding types in the System.Collections and System.Collections.Generic namespaces, when multiple threads are accessing the collection simultaneously. The classes present in this collection are:
sknagadiya
CSharp-Collections
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between Abstract Class and Interface in C#
C# | IsNullOrEmpty() Method
C# | How to check whether a List contains a specified element
String.Split() Method in C# with Examples
C# | Arrays of Strings
Difference between Ref and Out keywords in C#
Extension Method in C#
C# | String.IndexOf( ) Method | Set - 1
C# | Abstract Classes
C# | Delegates