12.30
I have started to read the book “More Effective C# by Bill Wagner” one of the early points mentioned is the fact that Value types used in closed generic definitions will have a greater memory hit at runtime relative to using reference types.
This is due to the fact that when at least one value type is used in a closed generic definition, the CLR when JIT-compiling a generic definition (either a method or a class) will create a separate “machine code page” for each value type definition e.g.
List<int> intList = new List<int>() List<RandomStruct> structList = new List<RandomStruct>()
Generic types that will be used with multiple different reference types do not affect the memory footprint, as a shared machine code page will be used e.g.
List<string> stringList = new List<string>() List<RandomClass> classList = new List<RandomClass>()
As the C# compiler will enforce type safety at compile time, the JIT compiler can produce a more optimized version of the machine code by assuming that the types are correct.
The reality is that Generics with value types provide many benefits that out weight the memory cost. These include the compile time type safety, the more concise code (no need to parse / cast).
When it comes to working in environments where every single little drop of memory is important such as mobile platforms, this little bit of knowledge may prove valuable, or indeed it may not, but ill leave that up to you to decide.



