How does C# generics affect collections with primitives

.net, c#, generics, reification

Solution

No, it won't be boxed. At execution time, the backing array for the `List<int>` will genuinely be an `int[]`. Note that this isn't just the case with genuine primitive types - `List<T>` won't box values of any value type (assuming it's been declared as `List<Guid>` etc rather than `List<object>`).

Basically, generics in .NET keep a lot more of their information than they do in Java - the CLR natively understands generics and deals with them appropriately, rather than in Java where the JVM is pretty much ignorant of them.

For example, if you write:

object list = new List<string>();
Type type = list.GetType();

Then `type` will be equal to `typeof(List<string>)` - which is then different to (say) `List<Guid>` etc.

Problem

As I understand it, C#/.Net generics support some degree of reification. So, if I have the following code: ``` List<int> list = new List<int>(); list.Add(1); ``` Will the value 1 be autoboxed or will the 'list' object handle primitive ints efficiently?

Original source