As far as I got to understand Structs are value types which means they are copied when they are passed around. So if you change a copy you are changing only that copy, not the original and not any other copies which might be around.
However, you might have a struct with a list of classes, of which you want value behaviour, for example:
private struct InitData {
public string name;
public float initialSpeed;
public bool loop;
public List<Settings> settings;
}
Settings is a class with a lot of simple types, that is a class due to need of reference behaviolur.
For my case I need to keep the initial value of a class, for which I need this struct to keep the data.
Simple types behave as expected, and they are not modified when I set the values from this struct, not so with the List<Settings> Settings
that changes values, because behaves as a reference type although it is inside a struct.
So I have to do to my _initData
struct instance and to my List<Settings> _settings
instance to break with the reference type behaviour of the list.
_settings = _initData.settings;
//renew instace to have value behaviour:
_initData.nodeSettings = new List<Settings>();
_initData.nodeSettings = (List<Settings>)DeepCopy(_settings.ToList());
So, as it does not seem very appropiate to generate a new instance of the list every single time, is there any way to achieve value behaviour inside a struct for a data structure to handle many instances or references with value type behaviour (meaning list, array, vector, etc.)?