C# Interview Question #33
What is a collection in C#?
Generics & Collections Mid-Level Intermediate
Quick Interview Answer
A collection is an object used to store and manage groups of values or objects. .NET provides generic collections in System.Collections.Generic for common data structures.
Detailed Explanation
A collection is an object used to store and manage groups of values or objects. .NET provides generic collections in System.Collections.Generic for common data structures.
Frequently used collections include List<T>, Dictionary<TKey,TValue>, HashSet<T>, Queue<T>, Stack<T>, and LinkedList<T>.
The correct collection should be chosen according to the required operations, such as indexed access, key lookup, uniqueness, FIFO processing, or LIFO processing.
Code Example
List<string> names = new() { "Ali", "Ahmed", "Sara" };
Dictionary<int, string> users = new()
{
[1] = "Ali",
[2] = "Sara"
};