简体   繁体   中英

C# Array or List for a single Class type

I have a class that contains a few basic types. (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.

If it would be C++, I know that the array would hold the complete object. But I'm not sure how C# handles this. Will a C# array only hold references to the class instances or will it hold the complete datastructure?

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> ? ImmutableArray<T> would make the most sense. You can use ImmutableArray<T>.Builder to create the collection in efficient way.

  • 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. 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> . 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. So yes, you can save memory using Array directly, because you won't have any unnecessary memory allocated. 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.

  • What about speed?

Using array you'll save logic that makes List<T> methods, properties and indexer property calls translated to underlying array calls. 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. But I'm not sure how C# handles this. Will a C# array only hold references to the class instances or will it hold the complete datastructure?

It depends. If you define your type as reference type (a class ) both array and list would just hold a reference to particular items. If you define it as value type (a struct ), array will hold the actual elements.

  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. 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.
  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).

In C++ Arrays and Lists not always store whole complete object inside it. They can contain contain just references. Same thing in .NET languages(true even for managed C++). Value-types will be stored as-is. Reference-types will be stored... as references to objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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