I came across the fact that indexer this[int index] { get; } this[int index] { get; } works differently for an array of structures than for a list of structures. Namely, that the indexer in the case of T[] returns a reference to the element inside the array, while the indexer in the case of List<T> returns a copy of the element.
This is a very big semantics and performance difference, and I'm glad that T[] allows you to bypass the performance limitation of List<T> .
However, I am puzzled by the actual implementation. The code for Array in .net source reads this way:
Object IList.this[int index] { get { return GetValue(index); } set { SetValue(value, index); } }
Where GetValue is defined as follows:
public unsafe Object GetValue(int index) { if (Rank != 1) throw new ArgumentException(Environment.GetResourceString("Arg_Need1DArray")); Contract.EndContractBlock(); TypedReference elemref = new TypedReference(); InternalGetReference(&elemref, 1, &index); return TypedReference.InternalToObject(&elemref); }
The return type of the Object indexer, implying that boxing will take place.
So my question is: can I be sure that no boxing will happen when I get access to the element T[] , where T is a structure?
I assume that the compiler and / or CLR are specific to the array, and in fact the signature of the indexer does not bother. It's right? Is there a more complete discussion of this somewhere?
source share