Definition
- **IList<T>**: Represents a generic collection of objects accessible by index. It supports adding, removing, and modifying elements and extends ICollection<T>.
-
IDictionary<TKey, TValue>: Represents a collection of key/value pairs with unique keys, enabling fast lookup, addition, and removal by key. It extends ICollection<KeyValuePair<TKey, TValue>>.
Features
- IList
- Supports zero-based indexing
- Provides methods like Insert, RemoveAt, IndexOf
- Allows accessing elements by position
- Implements ICollection<T> and IEnumerable<T>
- IDictionary<TKey, TValue>
- Stores pairs with unique keys, values can be duplicated
- Enables fast retrieval via keys
- Supports Add, Remove, TryGetValue methods
- Implements ICollection<KeyValuePair<TKey, TValue>>
2. Collection Classes
2.1 ArrayList
-
Non-generic dynamic array
-
Stores elements as object type (boxed)
-
Can store heterogeneous types
-
Less type-safe, needs casting
-
Provides Add, Remove, Insert methods
2.2 Hashtable
-
Non-generic key-value collection
-
Keys are hashed for fast lookup
-
Keys must be unique
-
Values can be null or duplicated
-
Not type-safe, uses object references
2.3 Stack
-
LIFO (Last-in, First-out) collection
-
Supports Push, Pop, Peek methods
-
Can be generic Stack
or non-generic Stack -
Useful for undo operations, recursive algorithms
2.4 Queue
-
FIFO (First-in, First-out) collection
-
Supports Enqueue, Dequeue, Peek methods
-
Can be generic Queue
or non-generic Queue -
Useful for scheduling, buffering
3. Writing Custom Generic Classes
Definition
-
Custom generic classes enable reusable, type-safe containers and algorithms
-
Uses type parameters
<T>for generalization
Example
public class MyGenericList<T>
{
private T[] elements;
private int count = 0;
public MyGenericList(int size)
{
elements = new T[size];
}
public void Add(T item)
{
if (count < elements.Length)
{
elements[count++] = item;
}
else
{
throw new InvalidOperationException("List is full");
}
}
public T Get(int index)
{
if (index >= 0 && index < count)
return elements[index];
else
throw new IndexOutOfRangeException();
}
}
4. Working with Generic Collection Classes
Common Generic Collections
-
List
: Dynamic array, supports fast indexed access -
Dictionary<TKey, TValue>: Key-value pairs with unique keys
-
HashSet
: Unique elements only, no duplicates -
LinkedList
: Doubly linked list -
SortedList<TKey, TValue>: Sorted key-value pairs
Features
-
Type-safe and more performant due to no boxing/unboxing
-
Rich API for manipulation (Add, Remove, Find, Sort)
-
Iteration with
foreach -
LINQ integration for queries
Example: Using List
List<string> fruits = newList<string>();
fruits.Add("Apple");
fruits.Add("Banana");
foreach(string fruit in fruits)
{
Console.WriteLine(fruit);
}
Example: Using Dictionary<TKey, TValue>
Dictionary<int, string> employees = new Dictionary<int, string>();
employees.Add(101, "John");
employees.Add(102, "Jane");
if(employees.TryGetValue(101, out string name))
{
Console.WriteLine($"Employee 101: {name}");
}
This format follows your specified note-taking pattern with clear headers, numbered and bulleted lists, and fully functional, concise code examples.
Footnotes
-
https://stackoverflow.com/questions/23387952/how-do-i-bind-a-ilist-of-idictionary-objects-to-the-collection-of-a-data-source ↩
-
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.idictionary-2?view=net-9.0 ↩
-
https://dev.to/scorpio69/data-structures-enumerable-enumerator-collection-list-dictionary-arrays-arraylist-hashtable-stack-queue-2dbe ↩
-
https://www.c-sharpcorner.com/UploadFile/dacca2/important-interface-in-net-work-with-idictionary-interface/ ↩
-
https://www.geeksforgeeks.org/c-sharp/c-sharp-program-to-demonstrate-the-ilist-interface/ ↩
-
https://learn.microsoft.com/en-us/dotnet/standard/collections/commonly-used-collection-types ↩
-
https://blog.stackademic.com/mastering-collections-in-c-21f855f5f7f3 ↩