How do non-generic collections like ArrayList and Hashtable differ from their generic counterparts (List<T>, Dictionary<TKey, TValue>)?
Feature Non-Generic Generic
Type Safety No Yes
Performanc
Slower (boxing/unboxing) Faster
Casting Required Not required
Syntax Less readable Clean and
type-specific
Examples:
// Non-generic
ArrayList arr = new ArrayList();
arr.Add(1);
arr.Add("text"); // Allowed, but risky
// Generic
List<int> list = new List<int>();
list.Add(1);
// list.Add("text"); // Compile-time error
// Hashtable vs Dictionary
Hashtable ht = new Hashtable();
ht["id"] = 101;
Dictionary<string, int> dict = new Dictionary<string, int>();
dict["id"] = 101;
Real-world use case:
Generic collections are recommended for new development due to safety and performance.
Non-generic collections are often found in older legacy systems.
Follow: