Array indexer signature returns object - does it box?
<p>I stumbled on the <a href="https://stackoverflow.com/questions/7133859/get-a-reference-to-a-struct-inside-array">fact</a> that the indexer <code>this[int index] { get; }</code> works differently for an array of structs than it does for a List of structs. Namely, that the indexer in the case of an <code>T[]</code> returns a reference to the element within the array whereas the indexer in the case of a <code>List&lt;T&gt;</code> returns a copy of the element.</p>

<p>This is a very big semantic and performance difference, and I'm glad that <code>T[]</code> allows us to work around the performance limitation of <code>List&lt;T&gt;</code>.</p>

<p>However, I'm puzzled by the actual implementation. The <a href="http://referencesource.microsoft.com/#mscorlib/system/array.cs,251c99c8419d33d4" rel="nofollow noreferrer">code</a> for <code>Array</code> in the .net reference sources reads thus:</p>

<pre><code>Object IList.this[int index] {
    get { return GetValue(index); }
    set { SetValue(value, index); }
}
</code></pre>

<p>Where <code>GetValue</code> is defined as follows:</p>

<pre><code>public unsafe Object GetValue(int index)
{
    if (Rank != 1)
       throw new ArgumentException(Environment.GetResourceString("Arg_Need1DArray"));
    Contract.EndContractBlock();
    TypedReference elemref = new TypedReference();
    InternalGetReference(&amp;elemref, 1, &amp;index);
    return TypedReference.InternalToObject(&amp;elemref);
}
</code></pre>

<p>The return type of the indexer is <code>Object</code>, implying that boxing will take place. </p>

<p>So my question is, can I be certain that no boxing will occur when I access an element of a <code>T[]</code> where <code>T</code> is a struct?</p>

<p>I assume that the compiler and/or CLR treat Array specially, and don't actually bother with the signature of the indexer. Is this correct? Is there a fuller discussion of this somewhere?</p>