How does C# generics affect collections with primitives

后端 未结 4 1721
情歌与酒
情歌与酒 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:42

    As others have noted, the jitter generates new code for every construction involving a new value type. An interesting point not yet mentioned so far is that the jitter will generate code once for a reference type construction and re-use that for every reference type. The code for List is exactly the same as the code for List.

    That might sound crazy, but remember, generics are not templates. By the time the code for the generic method body IL is emitted, overload resolution and other relevant semantic analysis is already done by the C# compiler.

    提交回复
    热议问题