Collections C#
Collections come in two main types:
- generic collections (type-safe)
- non-generic collections.
Choosing the Right Collection
|
Requirement |
Recommended
Collection |
|
Fast lookups with
key-value pairs |
|
|
Maintain insertion order |
|
|
No duplicates |
|
|
FIFO behavior |
|
|
LIFO behavior |
|
|
Observable for changes |
|
|
Sorted collection |
|
|
Efficient insertion/deletion |
|
Performance Considerations
- Non-Generic vs. Generic:
Generic collections perform better due to type-safety and avoidance of boxing/unboxing. - Memory Usage:
Collections like Dictionary<TKey, TValue> consume more memory than arrays due to hashing. - Lookup Speed:
Generic Collections vs Non-Generic Collections in C#: Key
Differences
|
Feature |
Generic Collections |
Non-Generic
Collections |
|
Namespace |
System.Collections.Generic |
System.Collections |
|
Type-Safety |
Type-safe; ensures only specific
data types are allowed. |
Not type-safe; allows mixed data
types. |
|
Performance |
Faster
due to type safety and no boxing/unboxing. |
Slower
due to boxing/unboxing for value types. |
|
Compile-Time Checks |
Errors are caught at compile-time
for type mismatches. |
Errors occur at runtime due to
type mismatches. |
|
Flexibility |
Stores
a specific type of data, ensuring consistency. |
Can
store any type of data, offering more flexibility. |
|
Examples |
List<T>, Dictionary<TKey, TValue>, HashSet<T> |
ArrayList, Hashtable,
Queue, Stack |
|
Boxing/Unboxing |
Not
required; directly operates on the specific type. |
Required
for value types, leading to additional overhead. |
|
Usability with LINQ |
Fully compatible; supports LINQ
queries. |
Limited or requires conversion to
a generic collection. |
|
Code
Readability and Safety |
Easier
to read and safer due to strong typing. |
More
prone to runtime errors and type-related bugs. |
Performance Comparison
Boxing and
Unboxing Overhead
- Generic Collections: Do
not involve boxing/unboxing for value types because they store specific
types directly.
- Non-Generic Collections:
Require boxing (converting a value type to an object) and unboxing
(converting an object back to a value type), which is computationally
expensive.
|
//
Example of boxing/unboxing in ArrayList (Non-Generic) ArrayList
list = new ArrayList(); list.Add(10); // Boxing occurs int
number = (int)list[0]; // Unboxing
occurs |
// No boxing/unboxing in List<T> (Generic) List<int> numbers = new List<int>(); numbers.Add(10); // Direct addition of int int number = numbers[0]; // No unboxing required |
Runtime vs
Compile-Time Type Checking
- Generic Collections:
Errors like type mismatches are caught at compile time, improving
performance and reducing runtime crashes.
- Non-Generic Collections:
Type mismatches are only detected at runtime, potentially causing
exceptions and additional overhead.
Performance Metrics
- Memory Usage: Generic collections use
memory more efficiently as they avoid the boxing/unboxing overhead.
- Speed: Generic collections are
faster for value types due to direct type handling.
- Error Handling: Generic collections provide
better compile-time error checking, reducing the need for additional
runtime validation.
When to Use
|
Use Generic Collections When |
Use Non-Generic Collections When |
|
You know the type
of data being stored. |
Working with legacy
code that uses non-generic collections. |
|
Performance is a priority. |
Need
flexibility to store mixed data types. |
|
You want
compile-time type checking for safety and clarity. |
No strong type
constraints are required. |
Choosing the Right Collection
|
Requirement |
Recommended
Collection |
|
Fast lookups with
key-value pairs |
|
|
Maintain insertion order |
|
|
No duplicates |
|
|
FIFO behavior |
|
|
LIFO behavior |
|
|
Observable for changes |
|
|
Sorted collection |
|
|
Efficient insertion/deletion |
|

Comments
Post a Comment