Govt. Exams
Entrance Exams
var dict = new Dictionary();
dict[1] = "A";
dict[1] = "B";
Console.WriteLine(dict[1]);
Using the indexer on Dictionary with an existing key updates the value, not throws. Output is 'B'.
HashSet<T> provides O(1) average lookup and prevents duplicates. SortedSet maintains order but slower. List<T> requires O(n) lookup.
If you have a LinkedListNode<T>, removal is O(1). However, finding the node is O(n), so overall search + remove is O(n).
ConcurrentDictionary is thread-safe. While it doesn't strictly preserve insertion order like some collections, it's the thread-safe option among these choices.
HashSet<T> uses hash codes for O(1) average lookup. It computes hash code first, then uses equality comparison.
List<T> is not thread-safe. All Concurrent* collections (ConcurrentDictionary, ConcurrentBag, ConcurrentQueue) are thread-safe.
SortedList uses arrays (faster retrieval, slower insertion). SortedDictionary uses red-black tree (balanced insertion/deletion). SortedDictionary has better insertion time O(log n) vs O(n) for SortedList.
First() throws InvalidOperationException if no match. FirstOrDefault() returns null/default. Find() is a List<T> method.
Dictionary<TKey, TValue> throws ArgumentException when adding duplicate keys. Use indexer notation to update values.
PriorityQueue<T, TPriority> was introduced in .NET 6 specifically for priority queue implementation.