
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# - Collections
Collection classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces.
Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items on the basis of an index etc. These classes create collections of objects of the Object class, which is the base class for all data types in C#.
C# provides various collection classes, the important collection classes are:
- ArrayList
- Hashtable
- SortedList
- Stack
- Queue
ArrayList Collection
An ArrayList is a non-generic collection that can store elements of any data type. It resizes dynamically based on the number of elements added.
Example
This example demonstrates how to use an ArrayList to store and display elements of different types:
using System; using System.Collections; class Program { static void Main() { ArrayList list = new ArrayList(); list.Add(1); list.Add("Hello"); list.Add(3.14); foreach (var item in list) { Console.WriteLine(item); } } }
When the above code is compiled and executed, it produces the following result −
1 Hello 3.14
Hashtable Collections
A Hashtable is a non-generic collection which stores key-value pairs.
Example
This example demonstrates how to create a Hashtable and retrieve values using keys:
using System; using System.Collections; class Program { static void Main() { Hashtable hashtable = new Hashtable(); hashtable.Add("ID", 101); hashtable.Add("Name", "Sudhir"); hashtable.Add("Age", 30); Console.WriteLine($"ID: {hashtable["ID"]}"); Console.WriteLine($"Name: {hashtable["Name"]}"); Console.WriteLine($"Age: {hashtable["Age"]}"); } }
When the above code is compiled and executed, it produces the following result −
ID: 101 Name: Sudhir Age: 30
SortedList Collections
A SortedList is a collection of key-value pairs that are automatically sorted by the keys.
Example
This example demonstrates how a SortedList stores and displays elements in sorted order of keys:
using System; using System.Collections; class Program { static void Main() { SortedList sortedList = new SortedList(); sortedList.Add(3, "Three"); sortedList.Add(1, "One"); sortedList.Add(2, "Two"); foreach (DictionaryEntry item in sortedList) { Console.WriteLine($"{item.Key}: {item.Value}"); } } }
When the above code is compiled and executed, it produces the following result −
1: One 2: Two 3: Three
Stack Collections
A Stack collection follows last-in, first-out (LIFO) approach that means the items are added to and removed from the top of the stack.
Example
This example demonstrates how to push items onto a stack and pop them in reverse order:
using System; using System.Collections; class Program { static void Main() { Stack stack = new Stack(); stack.Push("First"); stack.Push("Second"); stack.Push("Third"); while (stack.Count > 0) { Console.WriteLine(stack.Pop()); } } }
When the above code is compiled and executed, it produces the following result −
Third Second First
Queue Collections
A Queue collection follows first-in, first-out (FIFO) approach that means the items are enqueued (added) at the end and dequeued (removed) from the front.
Example
This example demonstrates how to enqueue items into a queue and dequeue them in the same order:
using System; using System.Collections; class Program { static void Main() { Queue queue = new Queue(); queue.Enqueue("First"); queue.Enqueue("Second"); queue.Enqueue("Third"); while (queue.Count > 0) { Console.WriteLine(queue.Dequeue()); } } }
When the above code is compiled and executed, it produces the following result −
First Second Third
Various Collection Classes and Their Usage
The following are the various commonly used classes of the System.Collection namespace. Click the following links to check their detail.
Sr.No. | Class & Description and Useage |
---|---|
1 |
ArrayList
It represents ordered collection of an object that can be indexed individually. It is basically an alternative to an array. However, unlike array you can add and remove items from a list at a specified position using an index and the array resizes itself automatically. It also allows dynamic memory allocation, adding, searching and sorting items in the list. |
2 |
Hashtable
It uses a key to access the elements in the collection. A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection. |
3 |
SortedList
It uses a key as well as an index to access the items in a list. A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key , it is a Hashtable. The collection of items is always sorted by the key value. |
4 |
Stack
It represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. |
5 |
Queue
It represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue and when you remove an item, it is called deque. |
6 |
BitArray
It represents an array of the binary representation using the values 1 and 0. It is used when you need to store the bits but do not know the number of bits in advance. You can access items from the BitArray collection by using an integer index, which starts from zero. |