C# Generic vs Non-Generic: Type Safety and Performance
Compare generic and non-generic types in C#, including type safety, boxing overhead, code clarity, and when to choose each approach.

In C#, generic types such as List<T>, Dictionary<TKey, TValue>, and Func<T> have one or more type parameters. The compiler treats the type parameter as a placeholder, and the CLR substitutes a concrete type when the generic type is constructed, which gives strongly typed code. Non-generic types such as ArrayList, Hashtable, Queue, and Stack have no type parameters and store elements as object.
The choice affects type safety, runtime performance, and maintainability. In most new code, generic types are the better option. The difference is not just syntactic. It changes how the compiler validates your code, how the runtime allocates memory, and how you handle type conversions. When you write ArrayList numbers = new ArrayList(); numbers.Add(5);, the integer 5 is boxed into an object and stored on the heap. When you read it back, you need to unbox it and cast it to int. With List<int>, the value type is stored directly without boxing, and the compiler knows the element type, so you do not need a cast.
Type Safety and Compile-Time Checks
The most immediate benefit of generics is compile-time type safety. With a non-generic collection, you can accidentally add a string to an ArrayList that you intend to hold integers. The compiler allows it because everything is an object. The error appears later, at runtime, when you try to cast the element back to int and get an InvalidCastException.
Generic collections prevent this class of bug. List<int> only accepts int values. If you try to add a string, the code does not compile. This moves the check from runtime to compile time, which reduces the chance of production failures and makes the code easier to reason about.
Consider this example:
// Non-generic: compiles, but fails at runtime
ArrayList list = new ArrayList();
list.Add(10);
list.Add("text"); // allowed
int first = (int)list[0]; // ok
int second = (int)list[1]; // InvalidCastException// Generic: fails at compile time
List<int> list = new List<int>();
list.Add(10);
list.Add("text"); // compile-time error
int first = list[0]; // no cast neededThe generic version gives you immediate feedback in the IDE and the compiler, and it makes the intent of the collection explicit.
Performance and Boxing/Unboxing
When you store value types (such as int, double, struct) in a non-generic collection, the runtime boxes them. Boxing allocates a new object on the heap and copies the value into it. Reading the value back requires unboxing, which checks the type and copies the value out. These operations add CPU and memory overhead, especially in loops that process large amounts of data.
Generic collections avoid boxing for value types because the type parameter is known and the storage is specialized. List<int> stores the integers in a contiguous array of int, not an array of object. This reduces memory allocation and improves cache locality.
The performance difference is not just theoretical. In a tight loop that adds and reads millions of integers, the generic version can be significantly faster and produce less garbage. However, the exact numbers depend on the workload, the runtime version, and the machine. The key point is that generics eliminate the boxing/unboxing overhead for value types, which is a real cost in non-generic collections.
For reference types, the difference is smaller because the object reference is already a pointer and does not need boxing. But you still lose type safety and need casts with non-generic collections.
Code Reusability and Maintainability
Generics allow you to write a single method or class that works with many types without sacrificing type safety. For example, a generic method to find the maximum of two values:
public static T Max<T>(T a, T b) where T : IComparable<T>
{
return a.CompareTo(b) > 0 ? a : b;
}You can call Max(3, 5) or Max("apple", "pear") and get a strongly typed result. With a non-generic approach, you would either overload the method for each type or use object and cast, which is error-prone and less readable.
Generic collections also make the code self-documenting. List<Customer> tells you the list holds customer objects. ArrayList does not. When you revisit the code later, you have to inspect the usage to understand the intended element type. This maintenance burden grows with the size of the codebase.
Common Non-Generic Types in .NET
The .NET Base Class Library includes several non-generic collections that were common before generics were introduced in .NET 2.0. These include:
ArrayList– a dynamic array that storesobjectelements.Hashtable– a key-value collection where keys and values areobject.Queue– a first-in, first-out collection.Stack– a last-in, first-out collection.
These types are still available for backward compatibility, but they are rarely the best choice for new code. The generic equivalents are List<T>, Dictionary<TKey, TValue>, Queue<T>, and Stack<T>. The generic versions provide the same functionality with type safety and better performance for value types.
There is also System.Collections.Specialized for specialized non-generic collections, but the same principle applies.
When to Use Generic vs Non-Generic
The decision is not always one-sided. In most modern C# code, you should prefer generic collections and generic methods. They give you compile-time type safety, better runtime performance for value types, and clearer code.
There are a few scenarios where non-generic types might still appear:
- Legacy code: If you are maintaining an older codebase that already uses
ArrayListorHashtable, you may not want to refactor everything at once. You can gradually migrate to generics as you touch the relevant code. - Reflection and dynamic scenarios: When you do not know the element type until runtime, such as when you are building a collection dynamically from metadata, a non-generic collection can be simpler. However, you can also use
List<object>orList<dynamic>in many cases. - Interoperability: Some older APIs or COM interop may require non-generic collections. In those cases, you have to use them at the boundary, but you can still convert to generic types internally.
In general, if you are writing new code, choose generic types. The only reason to use non-generic types is compatibility with an existing API or a very specific dynamic scenario.
| Criterion | Generic | Non-Generic |
|---|---|---|
| Type safety | Compile-time | Runtime |
| Performance for value types | No boxing | Boxing overhead |
| Code clarity | Explicit element type | Implicit object |
| Legacy compatibility | Modern | Legacy |
Practical Example: Generic List vs ArrayList
Let's compare a simple operation: adding and reading a set of integers.
// Non-generic
ArrayList arrayList = new ArrayList();
for (int i = 0; i < 1000; i++)
{
arrayList.Add(i);
}
int sum = 0;
foreach (object item in arrayList)
{
sum += (int)item; // unboxing
}// Generic
List<int> list = new List<int>();
for (int i = 0; i < 1000; i++)
{
list.Add(i);
}
int sum = 0;
foreach (int item in list)
{
sum += item; // no cast, no unboxing
}The generic version is shorter, does not require a cast, and avoids boxing/unboxing. The non-generic version is more verbose and has a hidden runtime cost. This example is small, but the difference grows with larger data sets and more complex value types.
Runtime Behavior and Compatibility
Generic type definitions are reified by the CLR when a constructed type is used. For each value type used as a type argument, the runtime creates specialized code that stores and operates on that value directly. For reference types, the CLR can share the same compiled code because the storage is just a pointer, but the metadata and compile-time checks still track the element type.
Non-generic collections are ordinary classes that store object. They can hold any type, but they lose static type information and require casts when you read elements back. Generics are supported in all modern .NET languages, so language compatibility is not a reason to prefer non-generic collections.
Limitations and Edge Cases
Generics are not a silver bullet. They can add complexity in a few situations. For example, operators such as + are not available on an unconstrained type parameter. You need a constraint that provides the operation, such as a generic math interface in .NET 7 and later, or you must work around it with a delegate or interface method. You also cannot construct a new T without the new() constraint. These constraints preserve type safety but can make generic code more verbose.
Another edge case is variance. Generic interfaces and delegates support covariance and contravariance, but generic classes do not. You cannot assign List<Derived> to List<Base> directly; use IEnumerable<Base> or another covariant interface if you need that assignment. Non-generic collections avoid this limitation by storing object, but they also lose type safety.
Finally, if you work with very large value types, the performance benefit of generics may be less pronounced because copying the value itself can be expensive. In such cases, a class or a reference-type design might be a better fit. The right choice depends on the specific data and usage pattern.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.