Interview question and answer on C# Collection framework with full explanation and code example.
Q: What is a collection in C#? A: A collection in C# is a group of related objects that can be accessed and manipulated as a single unit. It provides various data structures and algorithms to store and manage data efficiently.
Q: What are the main types of collections in C#? A: The main types of collections in C# are:
- Lists: Ordered collection of elements.
- Arrays: Fixed-size collection of elements.
- Dictionaries: Key-value pairs collection.
- Queues: First-in, first-out (FIFO) collection.
- Stacks: Last-in, first-out (LIFO) collection.
- Sets: Unique collection of elements.
Q: Explain the List collection in C#. A: List is an ordered collection that allows duplicate elements. It provides dynamic resizing, indexing, searching, sorting, and other operations. Here's an example:
csharpList<int> numbers = new List<int>(); numbers.Add(1); numbers.Add(2); numbers.Add(3); Console.WriteLine(numbers[1]); // Output: 2
Q: What is the difference between an array and a List? A: Arrays have a fixed size, while Lists can dynamically resize. Arrays use square brackets for indexing, while Lists use the Indexer property. Arrays require manual resizing, while Lists handle resizing automatically.
Q: Explain the Dictionary collection in C#. A: Dictionary is a collection of key-value pairs. It provides fast lookup and retrieval of elements based on the key. Duplicate keys are not allowed. Here's an example:
csharpDictionary<string, int> ages = new Dictionary<string, int>(); ages.Add("John", 25); ages.Add("Alice", 30); Console.WriteLine(ages["John"]); // Output: 25
Q: What is the difference between a Dictionary and a Hashtable? A: Both Dictionary and Hashtable store key-value pairs, but Hashtable is a legacy class from earlier versions of .NET, while Dictionary is a generic class introduced in .NET 2.0. Dictionary provides type safety and better performance compared to Hashtable.
Q: Explain the Queue collection in C#. A: Queue is a collection that follows the FIFO (First-In, First-Out) principle. It allows adding elements at the end and removing elements from the front. Here's an example:
csharpQueue<string> names = new Queue<string>(); names.Enqueue("John"); names.Enqueue("Alice"); Console.WriteLine(names.Dequeue()); // Output: John
Q: Explain the Stack collection in C#. A: Stack is a collection that follows the LIFO (Last-In, First-Out) principle. It allows adding elements at the top and removing elements from the top. Here's an example:
csharpStack<string> names = new Stack<string>(); names.Push("John"); names.Push("Alice"); Console.WriteLine(names.Pop()); // Output: Alice
Q: Explain the HashSet collection in C#. A: HashSet is a collection that stores unique elements. It provides fast lookup and ensures that no duplicate elements are present. Here's an example:
csharpHashSet<int> numbers = new HashSet<int>(); numbers.Add(1); numbers.Add(2); numbers.Add(2); // Ignored (already exists) Console.WriteLine(numbers.Count); // Output: 2
Q: What is the difference between a List and a HashSet? A: Lists allow duplicate elements, while HashSets store unique elements only. Lists maintain the insertion order, while HashSets do not guarantee any specific order.
Q: Explain the SortedList collection in C#. A: SortedList is a collection that maintains the elements in sorted order based on the keys. It provides efficient searching, insertion, and removal of elements. Here's an example:
csharpSortedList<int, string> items = new SortedList<int, string>(); items.Add(3, "Item 3"); items.Add(1, "Item 1"); items.Add(2, "Item 2"); Console.WriteLine(items.Values[1]); // Output: Item 2
Q: Explain the LinkedList collection in C#. A: LinkedList is a collection that represents a doubly-linked list. It allows efficient insertion, removal, and traversal of elements. Here's an example:
csharpLinkedList<string> names = new LinkedList<string>(); names.AddLast("John"); names.AddLast("Alice"); Console.WriteLine(names.First.Value); // Output: John
Q: Explain the ObservableCollection collection in C#. A: ObservableCollection is a collection that provides notifications when the collection is modified. It is useful for data binding scenarios where UI elements need to be updated automatically. Here's an example:
csharpObservableCollection<string> names = new ObservableCollection<string>(); names.CollectionChanged += (sender, e) => { Console.WriteLine("Collection modified!"); }; names.Add("John");
- Q: Explain the BitArray collection in C#. A: BitArray is a collection that stores a compact array of Boolean values. It provides space-efficient storage and bitwise operations on the elements. Here's an example:
- csharp
BitArray bits = new BitArray(4); bits.Set(1, true); bits.Set(3, true); Console.WriteLine(bits.Get(3)); // Output: True
Q: How do you iterate over elements in a collection? A: You can use loops such as foreach or for to iterate over elements in a collection. Here's an example with a List:
csharpList<int> numbers = new List<int> { 1, 2, 3 }; foreach (int number in numbers) { Console.WriteLine(number); }
Q: How do you check if a collection contains a specific element? A: You can use the Contains method to check if a collection contains a specific element. Here's an example:
csharpList<string> names = new List<string> { "John", "Alice" }; bool containsJohn = names.Contains("John"); Console.WriteLine(containsJohn); // Output: True
Q: How do you sort elements in a collection? A: You can use the Sort method or the OrderBy LINQ extension method to sort elements in a collection. Here's an example with a List:
csharpList<int> numbers = new List<int> { 3, 1, 2 }; numbers.Sort(); Console.WriteLine(string.Join(", ", numbers)); // Output: 1, 2, 3
Q: How do you find the maximum or minimum element in a collection? A: You can use the Max or Min methods to find the maximum or minimum element in a collection. Here's an example:
csharpList<int> numbers = new List<int> { 3, 1, 2 }; int maxNumber = numbers.Max(); int minNumber = numbers.Min(); Console.WriteLine(maxNumber); // Output: 3 Console.WriteLine(minNumber); // Output: 1
Q: How do you remove elements from a collection? A: You can use methods like Remove, RemoveAt, or RemoveRange to remove elements from a collection. Here's an example with a List:
csharpList<string> names = new List<string> { "John", "Alice", "Bob" }; names.Remove("Alice"); Console.WriteLine(string.Join(", ", names)); // Output: John, Bob
Q: How do you clear all elements from a collection? A: You can use the Clear method to remove all elements from a collection. Here's an example:
csharpList<int> numbers = new List<int> { 1, 2, 3 }; numbers.Clear(); Console.WriteLine(numbers.Count); // Output: 0
Q: How do you convert a collection to an array? A: You can use the ToArray method to convert a collection to an array. Here's an example:
csharpList<int> numbers = new List<int> { 1, 2, 3 }; int[] array = numbers.ToArray(); Console.WriteLine(string.Join(", ", array)); // Output: 1, 2, 3
Q: How do you convert a collection to a string? A: You can use the string.Join method to concatenate the elements of a collection into a single string. Here's an example:
csharpList<string> names = new List<string> { "John", "Alice" }; string namesString = string.Join(", ", names); Console.WriteLine(namesString); // Output: John, Alice
Q: How do you check if a collection is empty? A: You can use the Count property or the Any method to check if a collection is empty. Here's an example:
csharpList<int> numbers = new List<int>(); bool isEmpty = numbers.Count == 0; Console.WriteLine(isEmpty); // Output: True
Q: How do you copy elements from one collection to another? A: You can use methods like AddRange or Concat to copy elements from one collection to another. Here's an example:
csharpList<int> numbers1 = new List<int> { 1, 2, 3 }; List<int> numbers2 = new List<int>(); numbers2.AddRange(numbers1); Console.WriteLine(string.Join(", ", numbers2)); // Output: 1, 2, 3
Q: How do you check if two collections are equal? A: You can use the SequenceEqual method to check if two collections have the same elements in the same order. Here's an example:
csharpList<int> numbers1 = new List<int> { 1, 2, 3 }; List<int> numbers2 = new List<int> { 1, 2, 3 }; bool areEqual = numbers1.SequenceEqual(numbers2); Console.WriteLine(areEqual); // Output: True
These are some of the top interview questions and answers related to the C# Collection framework. Remember to understand the concepts and practice implementing them to gain confidence in using collections effectively in your code.
0 Comments