0% found this document useful (0 votes)
3 views

Unit III Collections

C# collections, found in the System.Collections and System.Collections.Generic namespaces, provide flexible data management with built-in operations and dynamic resizing. They are categorized into non-generic, generic, and concurrent collections, each serving different needs such as type safety and thread safety. Generic collections enhance performance and memory usage, while concurrent collections are designed for safe multi-threaded operations.

Uploaded by

shalinidbtechcse
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit III Collections

C# collections, found in the System.Collections and System.Collections.Generic namespaces, provide flexible data management with built-in operations and dynamic resizing. They are categorized into non-generic, generic, and concurrent collections, each serving different needs such as type safety and thread safety. Generic collections enhance performance and memory usage, while concurrent collections are designed for safe multi-threaded operations.

Uploaded by

shalinidbtechcse
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C# Collections

Specialized classes offered by the System are called collections.


system.Collections and System.Collections.Generic namespaces are made to
effectively manage, store, and work with data sets. In contrast to arrays, collections
provide more flexibility with built-in operations like sorting, searching, and
editing, as well as dynamic resizing and sophisticated data structures. These
collections, which include stacks, queues, dictionaries, and lists, offer organized
methods for effectively managing data. Collections are a crucial component of
contemporary programming as C# optimizes memory utilization, guarantees type
safety, and enhances efficiency by utilizing generic collections.
In C#, collections offer more robust data structures than arrays, enabling safe
multi-threaded operations, effective searching, and dynamic expansion. For
efficiency and type safety, generic collections (List, Dictionary) are better, but
concurrent collections work best in situations involving several threads.
Types of Collections in C#
C# provides two main categories of collections:
1. Non-Generic Collections (from System.Collections)
2. Generic Collections (from System.Collections.Generic)
3. Concurrent Collections (from System.Collections.Concurrent)

1. Non-Generic Collections
These collections can store objects of different data types because they store elements as objects
(boxing/unboxing is required). They are useful when working with mixed data types but are less
efficient than generic collections.
Common Non-Generic Collections
Collection Description
ArrayList A dynamic array that automatically resizes when elements are added or removed.

Hashtable A key-value pair collection where keys are unique and hashed for fast retrieval.

Queue A FIFO (First-In-First-Out) collection used for sequential processing.


Collection Description

Stack A LIFO (Last-In-First-Out) collection used for reversing order.


SortedList A key-value pair collection that maintains keys in sorted order.

using System;
using System.Collections;
class Program
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add(10);
list.Add("Hello");
list.Add(3.14);

foreach (var item in list)


{
Console.WriteLine(item);
}
}
}

2. Generic Collections
Generic collections are type-safe and do not require boxing/unboxing, making them more
efficient.
Common Generic Collections
Collection Description
List<T> A dynamically sized list (like an ArrayList but type-safe).
Collection Description

Dictionary<TKey, TValue> A key-value pair collection with fast lookups.

Queue<T> A FIFO collection for type-safe elements.


Stack<T> A LIFO collection for type-safe elements.
SortedList<TKey, TValue> A key-value pair collection sorted by key.

HashSet<T> A collection of unique values (no duplicates).


LinkedList<T> A doubly linked list for efficient insertions and deletions.
Advantages of Generic Collections:
 Type Safety: Prevents runtime errors by enforcing type checks at compile-time.
 Performance: Avoids boxing/unboxing (no object conversions).
 Better Memory Usage: Reduces unnecessary memory allocations.
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
List<int> numbers = new List<int> { 10, 20, 30 };
numbers.Add(40);
numbers.Remove(20);

foreach (int num in numbers)


{
Console.WriteLine(num);
}
}
}
3. Concurrent Collections
Concurrent collections are thread-safe and used in multithreading scenarios.
Common Concurrent Collections
Collection Description
ConcurrentBag<T> Unordered collection optimized for parallel execution.
ConcurrentDictionary<TKey, TValue> A thread-safe version of Dictionary<TKey, TValue>.

ConcurrentQueue<T> A thread-safe FIFO collection.


ConcurrentStack<T> A thread-safe LIFO collection.
When to use Concurrent Collections?
 In multi-threaded applications where multiple threads access and modify data
concurrently.
 To avoid race conditions and ensure data integrity.
Using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
class Program
{
static void Main()
{
ConcurrentDictionary<int, string> dict = new ConcurrentDictionary<int,
string>();
dict.TryAdd(1, "A");
dict.TryAdd(2, "B");
Parallel.For(1, 10, i => dict.TryAdd(i, "Value" + i));
foreach (var pair in dict)
{
Console.WriteLine($"{pair.Key}: {pair.Value}");
}
}
}

You might also like