The initial list / array should be immutable, so I don't need to synchronize with other threads.
Did you consider an immutable collection instead of T[] or List<T> ? ImmutableArray<T> will make the most sense. You can use ImmutableArray<T>.Builder to create a collection in an efficient way.
- Am I benefiting from an array instead of a list?
If you do not need the number of elements to change, you should use Array. This will make it clear to everyone who looks at your code that you are not changing the number of elements.
- Save memory with an array?
It depends on how you created the List<T> . Internally, when you add items to the List<T> , one after another, the size of the base array is changed using a 2 * factor: if there is not enough space for the new item, then the current base array is replaced with the new one with twice the size. So yes, you can save memory using the array directly, because you will not have allocated unnecessary memory. However, you can achieve the same by using List<T> , either by creating it using a constructor that uses the bandwidth of the list, or by calling the TrimExcess method TrimExcess items are added to the list.
Using an array, you save the logic that makes the methods, properties, and properties of the List<T> property translated into calls to the underlying array. But you do not have to worry about it, it will be imperceptible.
If it is C ++, I know that the array will contain the complete object. But I'm not sure how C # handles this. Will the C # array contain references to instances of the class, or will it contain the full data structure?
It depends. If you specify your type as a reference type (a class ), then both the array and the list will simply contain a link to certain elements. If you define it as a value type (a struct ), the array will contain the actual elements.
source share