Reputation: 18563
The question raised in my mind after Is it possible to access a reference of a struct from a List to make changes? thread by reza.
So, consider the following struct
and interface
(definetely not very useful, but just to show the issue):
public interface IChangeStruct
{
int Value { get; }
void Change(int value);
}
public struct MyStruct : IChangeStruct
{
int value;
public MyStruct(int _value)
{
value = _value;
}
public int Value
{
get
{
return value;
}
}
public void Change(int value)
{
this.value = value;
}
}
MyStruct
implements IChangeStruct
, so we can change a boxed copy of it right in the heap without unboxing and replacing with a new one. This can be demostrated with the following code:
MyStruct[] l1 = new MyStruct[]
{
new MyStruct(0)
};
Console.WriteLine(l1[0].Value); //0
l1[0].Change(10);
Console.WriteLine(l1[0].Value); //10
Now, let's change array to List<T>
, i.e.:
List<MyStruct> l2 = new List<MyStruct>
{
new MyStruct(0)
};
Console.WriteLine(l2[0].Value); //0
l2[0].Change(10);
Console.WriteLine(l2[0].Value); //also 0
As far as I understood, in the first case l1[0]
returned the referense to the boxed struct, while in the second - it was smth else.
I also tried to disassemble this and found:
1) For MyStruct[]
:
IL_0030: ldelema Utils.MyStruct IL_0035: ldc.i4.s 10 IL_0037: call instance void Utils.MyStruct::Change(int32)
2) For List<MyStruct>
:
IL_007c: callvirt instance !0 class [mscorlib]System.Collections.Generic.List`1<valuetype Utils.MyStruct>::get_Item(int32)
IL_0081: stloc.s CS$0$0001
IL_0083: ldloca.s CS$0$0001
IL_0085: ldc.i4.s 10
IL_0087: call instance void Utils.MyStruct::Change(int32)
But I appeared to be not ready to interpret it well.
So, what did the List<T>
return? Or how do array and List<T>
return elements by index? Or is this only the case with value types and has nothing to do with reference types?
P.S.: I do understand that one must not change a value type instance, but the described issue made me understand, I never realized how List<T>
and array work.
Upvotes: 6
Views: 694
Reputation: 81347
The indexer of an array makes an element available to the following code in a manner similar to passing it as a ref
parameter. No mechanism exists in any .net language for any other type to behave likewise. Any other type which allows indexed access must expose a pair of methods, one of which makes a copy of the internally-stored data available to the caller's code, and one of which will, given a copy of some data from the caller's code, store that data in some fashion. This limitation is most visible with value types, but may in some cases also be problematic with reference types (e.g. it's possible to perform an Interlocked.ComapreExchange
on an element in a T[]
, but not on an element with a List<T>
).
If one is designing one's own collection types, one may ease the limitation on indexers by offering an ActOnItem
member, thus allowing code like MyFancyList.ActOnItem(4, (ref Point it) => {it.X += 4;});
. It may be helpful to provide a family of generic versions with different numbers of additional ref
parameters which would be passed through from the caller (e.g. MyFancyList.ActOnItem(4, (ref MyThing it, ref Thing newValue, ref Thing compareValue) => Threading.Interlocked.CompareExchange(ref it, newValue, compareValue);
) since use of such methods can avoid the need for lambdas to use captured variables.
Upvotes: 3
Reputation: 888283
.Net can address array elements in-place, using the ldelema
instruction (load address of array element).
This allows you to operate directly on array elements without copying them. (this is also why you can pass an array element as a ref
or out
parameter)
List<T>
has no such capability. Instead, list[i]
is just syntactic sugar for list.get_Item(i)
, which is a normal method call that returns a copy of the struct.
Upvotes: 9