在C#中,是一个List&LT值;结构>盒装?

编程入门 行业动态 更新时间:2024-10-22 11:06:00
本文介绍了在C#中,是一个List&LT值;结构>盒装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我声明包含一个结构类型的值的泛型列表:

Suppose I declare a generic List containing values of a struct type:

struct MyStruct { public MyStruct(int val1, decimal val2) : this() { Val1 = val1; Val2 = val2; } public int Val1 {get; private set;} public decimal Val2 {get; private set;} } List<MyStruct> list;

请问名单<>存储每个单独的值作为套装结构,在堆上单独分配?或者比它更聪明?

Does the List<> store each individual value as a boxed struct, allocated individually on the heap? Or is it smarter than that?

推荐答案

有没有拳击。

不,不会有拳击,这是泛型的主要设计目标之一。

"No, there will be no boxing. That was one of the main design goals of Generics."

HTTP://social.msdn。 microsoft/Forums/en-US/csharplanguage/thread/359cf58a-f53d-478e-bc31-1507e77c9454/

该值类型类专门也就是说,一个列表元素<;如果一个值类型用于T类型,编译器生成名单,LT的实现; T>。 code>列表< T> 对象没有被装箱可以使用元素之前,并创造了约500列表元素后,保存在记忆不是拳击列表元素比内存大用于生成类实现。

"If a value type is used for type T, the compiler generates an implementation of the List<T> class specifically for that value type. That means a list element of a List<T> object does not have to be boxed before the element can be used, and after about 500 list elements are created the memory saved not boxing list elements is greater than the memory used to generate the class implementation."

msdn.microsoft/en-us/library/6sh2ey19.aspx

更多推荐

在C#中,是一个List&LT值;结构&GT;盒装?

本文发布于:2023-11-06 06:49:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562981.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是一个   结构   List   amp   LT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!