简体   繁体   English

单个类类型的C#数组或列表

[英]C# Array or List for a single Class type

I have a class that contains a few basic types. 我有一个包含一些基本类型的类。 (3x float, 2x int). (3x float,2x int)。

Now I need a collection that can hold several million instances of this class. 现在我需要一个可以容纳这个类的数百万个实例的集合。 I need no derived types. 我不需要派生类型。 All elements are exactly from this single class. 所有元素都完全来自这个单独的类。 Further more the number of elements is fixed. 更多元素的数量是固定的。 In very rarely cases I plan to copy the whole list/array and modify the copy. 在极少数情况下,我计划复制整个列表/数组并修改副本。 The originally list/array shall be immutable, thus I don't need to syncronize with other threads. 最初的列表/数组应该是不可变的,因此我不需要与其他线程同步。

Now the question are: 现在的问题是:

  • Do I benefit from an Array instead of a List? 我是从数组而不是列表中受益吗?
  • Do I save memory using an Array? 我是否使用数组保存内存?
  • What about speed? 速度怎么样?

I read that a List in C# is internally implemented as Array as well. 我读到C#中的List也在内部实现为Array。

If it would be C++, I know that the array would hold the complete object. 如果它是C ++,我知道数组将保存完整的对象。 But I'm not sure how C# handles this. 但我不确定C#如何处理这个问题。 Will a C# array only hold references to the class instances or will it hold the complete datastructure? C#数组是仅保存对类实例的引用还是保存完整的数据结构?

The originally list/array shall be immutable, thus I don't need to synchronize with other threads. 最初的列表/数组应该是不可变的,因此我不需要与其他线程同步。

Did you consider an immutable collection instead of T[] or List<T> ? 您是否考虑过不可变集合而不是T[]List<T> ImmutableArray<T> would make the most sense. ImmutableArray<T>最有意义。 You can use ImmutableArray<T>.Builder to create the collection in efficient way. 您可以使用ImmutableArray<T>.Builder以高效的方式创建集合。

  • Do I benefit from an Array instead of a List? 我是从数组而不是列表中受益吗?

If you don't need the number of elements to change you should use Array. 如果您不需要更改元素的数量,则应使用Array。 It will make it clear to everyone who looks at your code that you're not changing the number of elements. 它会让每个查看代码的人都清楚地知道你没有改变元素的数量。

  • Do I save memory using an Array? 我是否使用数组保存内存?

It depends how you'd create the List<T> . 这取决于你如何创建List<T> Internally, when you add elements to List<T> one by one underlying array's size is changes using 2* multiplier: when there is not enough space for new element current underlying array is replaced by a new one with twice the size. 在内部,当您向List<T>添加元素时,逐个基础数组的大小是使用2 *乘数的变化:当没有足够的空间用于新元素时,当前的基础数组被替换为大小为两倍的新数组。 So yes, you can save memory using Array directly, because you won't have any unnecessary memory allocated. 所以,是的,您可以直接使用Array保存内存,因为您不会分配任何不必要的内存。 However, you can achieve the same using List<T> , either by creating it using constructor that takes list capacity or by calling TrimExcess method after all elements are added to the list. 但是,您可以使用List<T>实现相同的目的,方法是使用获取列表容量的构造函数创建它,或者在将所有元素添加到列表后调用TrimExcess方法。

  • What about speed? 速度怎么样?

Using array you'll save logic that makes List<T> methods, properties and indexer property calls translated to underlying array calls. 使用数组,您将保存逻辑,使List<T>方法,属性和索引器属性调用转换为基础数组调用。 But you shouldn't care about that, it will be unnoticeable. 但你不应该关心它,它将是不明显的。

If it would be C++, I know that the array would hold the complete object. 如果它是C ++,我知道数组将保存完整的对象。 But I'm not sure how C# handles this. 但我不确定C#如何处理这个问题。 Will a C# array only hold references to the class instances or will it hold the complete datastructure? C#数组是仅保存对类实例的引用还是保存完整的数据结构?

It depends. 这取决于。 If you define your type as reference type (a class ) both array and list would just hold a reference to particular items. 如果将类型定义为引用类型( class ),则数组和列表都只包含对特定项的引用。 If you define it as value type (a struct ), array will hold the actual elements. 如果将其定义为值类型( struct ),则数组将保存实际元素。

  1. Not really. 并不是的。 Internally lists are just plain arrays, and additionally counter for elements in List(not in array). 内部列表只是普通数组,另外还有列表中的元素(不在数组中)。 You only will have more poor functionality. 你的功能会更差。
  2. If you know count of elements at, and set it while list initialization, then you will not save any memory. 如果您知道元素的数量,并在列表初始化时设置它,那么您将不会保存任何内存。 Memory from using List usage is very little, and while list size is fixed it's not depend on count of elements. 使用List使用的内存很少,虽然列表大小是固定的,但它不依赖于元素的数量。 But if you don's specify exact list size, then size of internal array will be doubled every time when you add new element and count of List elements is bigger than internal array size. 但是如果你没有指定确切的列表大小,那么每次添加新元素时,内部数组的大小将加倍,并且List元素的数量大于内部数组大小。
  3. No. Most time it's same as working with plain arrays. 不。大多数时候它与使用普通数组相同。 Except internal array re-sizing times(but it's not too long, and will not happen when List is fixed). 除了内部数组重新调整大小时间(但它不会太长,并且在List修复时不会发生)。

In C++ Arrays and Lists not always store whole complete object inside it. 在C ++中,数组和列表并不总是将完整的对象存储在其中。 They can contain contain just references. 它们可以包含仅包含引用。 Same thing in .NET languages(true even for managed C++). .NET语言也是如此(即使对于托管C ++也是如此)。 Value-types will be stored as-is. 值类型将按原样存储。 Reference-types will be stored... as references to objects. 引用类型将被存储...作为对象的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM