1. 🔴 What is a collection in C#, and what are the different types?
What is a Collection?
A collection in C# is used to store and manage multiple objects or values in a single variable.
Collections provide operations such as Add, Remove, Search, Sort, and Iterate.
Types of Collections
|
Type |
Examples |
Description |
|---|---|---|
|
Non-Generic |
|
Stores values as |
|
Generic |
|
Type-safe collections |
|
Concurrent |
|
Thread-safe operations |
|
Specialized |
|
Designed for specific purposes |
Example
List<string> names = new List<string>();
names.Add("John");
names.Add("David");
names.Add("Peter");
foreach (string name in names)
{
Console.WriteLine(name);
}
Output:
John
David
Peter
Explanation
-
List<string>creates a collection that stores strings. -
Add()inserts an element. -
foreachiterates through the collection. -
The list can grow dynamically.
Key Points
-
Collections store multiple values.
-
Generic collections provide compile-time type safety.
-
Collections support searching, sorting, and iteration.
-
List<T>andDictionary<TKey,TValue>are commonly used. -
Most standard collections are not inherently thread-safe.
2. 🔴 What is the difference between generic and non-generic collections?
What are Generic Collections?
Generic collections store elements of a specified data type, providing compile-time type safety.
Non-generic collections generally store elements as object, allowing different data types.
Comparison
|
Generic Collections |
Non-Generic Collections |
|---|---|
|
Type-safe |
Not type-safe |
|
Uses |
Uses |
|
Avoids boxing for value types |
May require boxing/unboxing |
|
Better performance for value types |
May have additional overhead |
|
|
|
|
|
|
Example
Non-Generic Collection
ArrayList items = new ArrayList();
items.Add(10);
items.Add("Hello");
items.Add(20);
int number = (int)items[0];
Generic Collection
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
// numbers.Add("Hello"); // Compile-time error
int number = numbers[0];
Explanation
-
ArrayListallows both integers and strings. -
Retrieving an integer from
ArrayListrequires casting. -
List<int>accepts only integers. -
Generic collections detect type mismatches at compile time.
Key Points
-
Prefer generic collections in modern C#.
-
Generic collections reduce runtime casting errors.
-
Generic collections avoid boxing/unboxing when storing value types.
-
Non-generic collections are mainly encountered in legacy applications.
3. 🔴 What is the difference between an array and a collection?
What is an Array?
An array stores multiple elements of the same type in a fixed-size structure.
A collection such as List<T> can dynamically increase or decrease its size.
Comparison
|
Array |
Collection ( |
|---|---|
|
Fixed size |
Dynamic size |
|
|
|
|
Uses |
Uses |
|
Cannot directly add elements |
Supports |
|
Lower overhead |
Additional capacity-management overhead |
|
Best for fixed-size data |
Best for frequently changing data |
Example
Array
int[] numbers = { 10, 20, 30 };
Console.WriteLine(numbers.Length); // 3
numbers[0] = 100;
// Cannot directly add a fourth element.
List Collection
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
numbers.Add(40);
Console.WriteLine(numbers.Count); // 4
Explanation
-
An array's length is fixed after creation.
-
List<T>dynamically manages its internal storage. -
Add()allows new elements to be inserted. -
Both arrays and lists support index-based access.
Diagram
Array
+----+----+----+
| 10 | 20 | 30 |
+----+----+----+
Fixed Size = 3
List<int>
+----+----+----+
| 10 | 20 | 30 |
+----+----+----+
|
| Add(40)
v
+----+----+----+----+
| 10 | 20 | 30 | 40 |
+----+----+----+----+
Dynamic Size = 4
Key Points
-
Arrays are suitable when the number of elements is fixed.
-
List<T>is suitable when the number of elements changes. -
Arrays and lists both provide O(1) index access.
-
List<T>internally uses an array. -
A list may allocate additional capacity to support growth.
4. 🔴 What are the advantages of generic collections?
What are Generic Collections?
Generic collections allow developers to define the type of elements using a type parameter such as <T>.
For example, List<int> stores integers, while List<string> stores strings.
Main Advantages
|
Advantage |
Explanation |
|---|---|
|
Type Safety |
Prevents inserting incorrect data types |
|
Performance |
Avoids boxing/unboxing for value types |
|
Reusability |
Same collection implementation supports different types |
|
No Explicit Casting |
Retrieves elements in their specified type |
|
Maintainability |
Makes code easier to understand and maintain |
Example
List<int> employeeIds = new List<int>();
employeeIds.Add(101);
employeeIds.Add(102);
employeeIds.Add(103);
int id = employeeIds[0];
Console.WriteLine(id);
Output:
101
Explanation
-
List<int>accepts only integer values. -
No explicit casting is required.
-
Incorrect types are rejected during compilation.
-
Integer values are stored without boxing.
Key Points
-
Generic collections were introduced in .NET Framework 2.0.
-
They provide compile-time type safety.
-
They improve performance by avoiding boxing/unboxing for value types.
-
Common examples are
List<T>,Dictionary<TKey,TValue>,HashSet<T>, andQueue<T>. -
They are recommended for modern .NET applications.
Quick Interview Revision
|
Question |
Short Answer |
|---|---|
|
What is a collection? |
A structure used to store and manage multiple values. |
|
Generic vs Non-Generic? |
Generic collections provide type safety and avoid boxing for value types. |
|
Array vs List? |
Arrays have fixed size; lists have dynamic size. |
|
Advantages of Generics? |
Type safety, performance, reusability, and no explicit casting. |
Interview Tip: In modern .NET applications, prefer List<T> for dynamic lists, Dictionary<TKey,TValue> for key-value lookup, and HashSet<T> for unique values.