How does C# generics affect collections with primitives

后端 未结 4 1724
情歌与酒
情歌与酒 2020-12-19 01:44

As I understand it, C#/.Net generics support some degree of reification. So, if I have the following code:

List list = new List();
lis         


        
4条回答
  •  我在风中等你
    2020-12-19 02:45

    The int values will not be boxed within the list. This is one of the beauties with generics, that the compiler (more specifically the JIT-compiler, I believe) will construct a typed version of the List<> class, rather than storing the values as object. So it does not only enforce type safety through the exposed methods and properties, but it is genuinely typed in all aspects.

提交回复
热议问题