Interview question and answer on C# Generic collection with full explanation and code example
Question 1: What is a collection in C#?
Answer:
A collection in C# is a group of related objects. It provides a way to store, manipulate, and retrieve multiple items efficiently.
Question 2: What are the different types of collections in C#?
Answer:
C# provides several collection types, including ArrayList, List<T>, Stack<T>, Queue<T>, Dictionary<TKey, TValue>, HashSet<T>, and LinkedList<T>.
Question 3: What is the difference between an ArrayList and a List<T>?
Answer:
ArrayList is a non-generic collection that can store objects of any type, while List<T> is a generic collection that is type-safe and can store elements of a specific type.
Code Example:
C#ArrayList arrayList = new ArrayList();
arrayList.Add(10);
arrayList.Add("Hello");
List<int> list = new List<int>();
list.Add(10);
// list.Add("Hello"); // This will result in a compilation error.
Question 4: How can you sort elements in a List<T>?
Answer:
You can use the Sort
method or the OrderBy
extension method to sort elements in a List<T>.
Code Example:
C#List<int> numbers = new List<int> { 3, 1, 2 };
numbers.Sort(); // Sorts the list in ascending order
Console.WriteLine(string.Join(", ", numbers)); // Output: 1, 2, 3
List<int> sortedNumbers = numbers.OrderBy(x => x).ToList(); // Sorts the list using LINQ
Console.WriteLine(string.Join(", ", sortedNumbers)); // Output: 1, 2, 3
Question 5: What is a generic collection in C#?
Answer:
A generic collection is a type-safe collection that can store elements of a specific type. It allows you to define the type of elements the collection can hold at compile time.
Question 6: What is the purpose of using generic collections?
Answer:
Generic collections provide type safety, better performance, and ease of use compared to non-generic collections.
Question 7: How do you declare and initialize a List<T>?
Answer:
You can declare and initialize a List<T> using the following syntax:
C#List<T> list = new List<T>();
Question 8: How can you add elements to a List<T>?
Answer: You can use the Add
method to add elements to a List<T>.
Code Example:
C#List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Orange");
Question 9: How do you access elements in a List<T>?
Answer:
Elements in a List<T> can be accessed using the index operator ([]).
Code Example:
C#List<string> fruits = new List<string> { "Apple", "Orange", "Banana" };
string firstFruit = fruits[0]; // Accesses the first element
Console.WriteLine(firstFruit); // Output: Apple
Question 10: How can you remove elements from a List<T>?
Answer:
You can use the Remove
or RemoveAt
methods to remove elements from a List<T>.
Question 11: What is the difference between a Stack<T> and a Queue<T>?
Answer:
Stack<T> is a last-in, first-out (LIFO) collection, while Queue<T> is a first-in, first-out (FIFO) collection.
Question 12: How can you add elements to a Stack<T>?
Answer:
You can use the Push
method to add elements to a Stack<T>.
Code Example:
C#Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
Question 13: How can you remove elements from a Stack<T>?
Answer:
You can use the Pop
method to remove and retrieve the topmost element from a Stack<T>.
Code Example:
C#Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
int topElement = stack.Pop(); // Removes and retrieves the topmost element (2)
Question 14: How can you add elements to a Queue<T>?
Answer:
You can use the Enqueue
method to add elements to a Queue<T>.
Code Example:
C#Queue<string> queue = new Queue<string>();
queue.Enqueue("Red");
queue.Enqueue("Green");
Question 15: How can you remove elements from a Queue<T>?
Answer:
You can use the Dequeue
method to remove and retrieve the first element from a Queue<T>.
Code Example:
C#Queue<string> queue = new Queue<string>();
queue.Enqueue("Red");
queue.Enqueue("Green");
string firstElement = queue.Dequeue(); // Removes and retrieves the first element ("Red")
Question 16: What is a Dictionary<TKey, TValue>?
Answer:
Dictionary<TKey, TValue> is a collection that stores key-value pairs, where each key must be unique.
Question 17: How do you add elements to a Dictionary<TKey, TValue>?
Answer:
You can use the Add
method to add key-value pairs to a Dictionary<TKey, TValue>.
Code Example:
C#Dictionary<string, int> studentAges = new Dictionary<string, int>();
studentAges.Add("John", 20);
studentAges.Add("Jane", 22);
Question 18: How do you access elements in a Dictionary<TKey, TValue>?
Answer:
Elements in a Dictionary<TKey, TValue> can be accessed using the key.
Code Example:
C#Dictionary<string, int> studentAges = new Dictionary<string, int>();
studentAges.Add("John", 20);
studentAges.Add("Jane", 22);
int johnsAge = studentAges["John"]; // Accesses the value associated with the key "John"
Console.WriteLine(johnsAge); // Output: 20
Question 19: How can you check if a key exists in a Dictionary<TKey, TValue>?
Answer:
You can use the ContainsKey
method to check if a key exists in a Dictionary<TKey, TValue>.
Code Example:
C#Dictionary<string, int> studentAges = new Dictionary<string, int>();
studentAges.Add("John", 20);
studentAges.Add("Jane", 22);
bool hasKey = studentAges.ContainsKey("John"); // Checks if the key "John" exists
Console.WriteLine(hasKey); // Output: true
Question 20: How do you iterate over elements in a Dictionary<TKey, TValue>?
Answer:
You can use a foreach loop to iterate over elements in a Dictionary<TKey, TValue>.
Code Example:
C#Dictionary<string, int> studentAges = new Dictionary<string, int>();
studentAges.Add("John", 20);
studentAges.Add("Jane", 22);
foreach (KeyValuePair<string, int> kvp in studentAges)
{
Console.WriteLine($"Name: {kvp.Key}, Age: {kvp.Value}");
}
Question 21: What is a HashSet<T>?
Answer:
HashSet<T> is a collection that stores unique elements in no particular order.
Question 22: How do you add elements to a HashSet<T>?
Answer:
You can use the Add
method to add elements to a HashSet<T>.
Code Example:
C#HashSet<string> colors = new HashSet<string>();
colors.Add("Red");
colors.Add("Green");
Question 23: How can you check if an element exists in a HashSet<T>?
Answer:
You can use the Contains
method to check if an element exists in a HashSet<T>.
Code Example:
C#HashSet<string> colors = new HashSet<string>();
colors.Add("Red");
colors.Add("Green");
bool containsGreen = colors.Contains("Green"); // Checks if "Green" exists
Console.WriteLine(containsGreen); // Output: true
Question 24: How can you remove elements from a HashSet<T>?
Answer:
You can use the Remove
method to remove elements from a HashSet<T>.
Code Example:
C#HashSet<string> colors = new HashSet<string>();
colors.Add("Red");
colors.Add("Green");
bool removedGreen = colors.Remove("Green"); // Removes "Green" and returns true
Question 25: How do you clear all elements in a collection?
Answer:
You can use the Clear
method to remove all elements from a collection.
Code Example:
C#List<string> fruits = new List<string> { "Apple", "Orange", "Banana" };
fruits.Clear(); // Removes all elements from the list
These questions cover various aspects of collections and generic collections in C#. Understanding these concepts and being able to provide clear explanations and code examples will demonstrate your proficiency in C# programming during interviews.
0 Comments