Generics in C# – JIT
- The JIT compiler creates one machine code version of a generic class for all reference types
List<MyClass> myList = new List<MyClass>(); List<File> files = new List<File>(); List<string> strings = new List<string>();
The C# compiler enforces type safety at compile time, and the JIT compiler can produce a more optimized version of the machine code by assuming that the types are correct.
List<MyClass> myList = new List<MyClass>(); List<File> files = new List<File>(); List<int> numbers = new List<int>();
Generic types that will be used with multiple different reference types do not affect the memory footprint. All JIT-compiled code is shared. However, when closed generic types contain value types as parameters, that JIT-compiled code is not shared
- More Effective C# – 50 Specific Ways To Improve C#