I want to modify a field of a struct which is inside an array without having to set entire struct. In the example below, I want to set one field of element 543 in the array.
You could try to use a forwarding empty struct which does not hold the actual data but only keeps an index to a dataprovider object. This way you can store huge amounts of data without complicating the object graph. I am very certain that it should be quite easy in your case to replace your giant struct with a forwarding emtpy struct as long as you do not try to marshal it into unmanaged code.
Have a look at this struct. It can contain as much data inside it as you wish. The trick is that you do store the actual data in another object. This way you get reference semantics and the advantages of structs which consume less memory than class objects and faster GC cycles due to a simpler object graph (if you have many instances (millions) of them around).
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct ForwardingEmptyValueStruct
{
int _Row;
byte _ProviderIdx;
public ForwardingEmptyValueStruct(byte providerIdx, int row)
{
_ProviderIdx = providerIdx;
_Row = row;
}
public double V1
{
get { return DataProvider._DataProviders[_ProviderIdx].Value1[_Row]; }
}
public int V2
{
get { return DataProvider._DataProviders[_ProviderIdx].Value2[_Row]; }
}
}