C# Interview Questions

36 Questions
C# Programming

C# Programming

Web DevelopmentFrontendBackendGame Dev

Question 16

What are collections in C# and name some commonly used collections?

Answer:

Collections in C# are specialized data structures used to store, manage, and manipulate groups of objects. They provide more flexibility and functionality than arrays, such as dynamic resizing, sorting, searching, and other operations. Collections are part of the System.Collections, System.Collections.Generic, System.Collections.Concurrent, and System.Collections.Immutable namespaces.

Commonly Used Collections in C#

1. ArrayList

  • Namespace: System.Collections
  • Description: A dynamically resizable array that can store elements of any type (object).
  • Key Features: Supports dynamic resizing, can store mixed types, slower due to boxing/unboxing when using value types.
  • Example:
    ArrayList arrayList = new ArrayList();
    arrayList.Add(1);
    arrayList.Add("Hello");
    arrayList.Add(3.14);

2. List<T>

  • Namespace: System.Collections.Generic
  • Description: A strongly-typed, dynamically resizable list of objects.
  • Key Features: Type-safe, better performance compared to ArrayList for value types, supports LINQ.
  • Example:
    List<int> intList = new List<int>();
    intList.Add(1);
    intList.Add(2);
    intList.Add(3);

3. Dictionary<TKey, TValue>

  • Namespace: System.Collections.Generic
  • Description: A collection of key-value pairs.
  • Key Features: Fast lookup, insert, and remove operations by key.
  • Example:
    Dictionary<string, int> dictionary = new Dictionary<string, int>();
    dictionary["one"] = 1;
    dictionary["two"] = 2;
    dictionary["three"] = 3;

4. HashSet<T>

  • Namespace: System.Collections.Generic
  • Description: A collection of unique elements.
  • Key Features: Fast lookup and insertion, no duplicates allowed.
  • Example:
    HashSet<int> hashSet = new HashSet<int>();
    hashSet.Add(1);
    hashSet.Add(2);
    hashSet.Add(2); // This will not be added again

5. Queue<T>

  • Namespace: System.Collections.Generic
  • Description: A first-in, first-out (FIFO) collection of objects.
  • Key Features: Enqueue and Dequeue operations.
  • Example:
    Queue<string> queue = new Queue<string>();
    queue.Enqueue("first");
    queue.Enqueue("second");
    queue.Enqueue("third");
    string dequeued = queue.Dequeue(); // "first"

6. Stack<T>

  • Namespace: System.Collections.Generic
  • Description: A last-in, first-out (LIFO) collection of objects.
  • Key Features: Push and Pop operations.
  • Example:
    Stack<string> stack = new Stack<string>();
    stack.Push("first");
    stack.Push("second");
    stack.Push("third");
    string popped = stack.Pop(); // "third"

7. LinkedList<T>

  • Namespace: System.Collections.Generic
  • Description: A doubly-linked list of objects.
  • Key Features: Efficient insertions and deletions at any position.
  • Example:
    LinkedList<string> linkedList = new LinkedList<string>();
    linkedList.AddLast("first");
    linkedList.AddLast("second");
    linkedList.AddFirst("zero");

8. ConcurrentDictionary<TKey, TValue>

  • Namespace: System.Collections.Concurrent
  • Description: A thread-safe collection of key-value pairs.
  • Key Features: Designed for concurrent access by multiple threads.
  • Example:
    ConcurrentDictionary<string, int> concurrentDictionary = new ConcurrentDictionary<string, int>();
    concurrentDictionary["one"] = 1;
    concurrentDictionary["two"] = 2;

9. ObservableCollection<T>

  • Namespace: System.Collections.ObjectModel
  • Description: A dynamic collection that provides notifications when items get added, removed, or when the entire list is refreshed.
  • Key Features: Useful for data binding in WPF and other XAML-based applications.
  • Example:
    ObservableCollection<string> observableCollection = new ObservableCollection<string>();
    observableCollection.Add("first");
    observableCollection.Add("second");

Summary

Collections in C# provide a variety of ways to store, manage, and manipulate groups of objects, each with its own set of features and use cases. Choosing the right collection depends on the specific requirements of your application, such as the need for type safety, performance considerations, or thread safety.

Recent job openings