How does C# generics affect collections with primitives

自闭症网瘾萝莉.ら 提交于 2019-11-28 03:05:12

问题


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?


回答1:


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.




回答2:


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.




回答3:


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<object> is exactly the same as the code for List<string>.

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.




回答4:


.NET generics are getting specialized for structs, thus there is no boxing required in your case. Note that there is no need for casting too anyway.



来源:https://stackoverflow.com/questions/3741350/how-does-c-sharp-generics-affect-collections-with-primitives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!