Ok, let's have some code:
//I complie nicely
ValueType[] good = new ValueType[1];
good[0].Name = "Robin";
//I don't compile: Cannot modify expression because its not a variable
IList<ValueType> bad = new ValueType[1];
bad[0].Name = "Jerome";
struct ValueType
{
public string Name;
}
What precisely is going on behind the scenes that is causing the compiler to baulk?
//Adding to watch the following
good.GetType().Name //Value = "ValueType[]" It's a ValueType array.
bad.GetType().Name //Value = "ValueType[]" Also a ValueType array.
The compiler is stopping me from modifying a member of the copy of the object I want to change. But why is a copy being made from this array?
A little more research throws up:
var guess = (ValueType[]) bad;
guess[0].Name="Delilah";
Now, what do you think bad[0].Name
is? That's right, it's "Delilah".