Home > Software design >  Are Structs with Struct-Arrays value or reference-based?
Are Structs with Struct-Arrays value or reference-based?

Time:08-01

If I have a Struct with arrays of other structs, is this than still a value-based type? In detail what I don´t understand is, that usually structs are meant to be value-based, but arrays for example are reference-based, right? So is such a struct now value-based or reference-based? And what does this mean in case of multithreading?

To put this in a applicable context:

If I generate a struct instance (which contains arrays of structs) within a Thread, read it from another Thread and assign it there to a property, can I than change and reuse this newly assigned property without conflicting with the struct instance from the original thread?

CodePudding user response:

A structure is a value type and a class is a reference type. That never changes.

If you have a local variable in a method in your code, when that code is executed, space is allocated for that variable on the stack. If that variable is a value type then the structure instance will be stored in the variable itself while, if the variable is a reference type, space will be allocated for the object on the heap and a reference to that object will be stored in the variable.

When an object is created, whether on the stack or the heap, that object contains its member variables. If the object is created on the stack then the member variables exist on the stack and if the object is created on the heap then the member variables exist on the heap. Whether those member variables exist on the stack or the heap, they still behave exactly as value types and reference types always do, i.e. the value type variables contain the objects and the reference type variables contain references to objects created on the heap.

If you have a structure with a member variable that is an array then the structure will behave like value types always do, i.e. the object will be stored in the variable, wherever that variable happens to be. The array field will contain a reference to an array created on the heap. If the array is of a value type then the array will contain the element objects themselves while an array that is of a reference type will contain references to objects stored elsewhere on the heap.

It's pretty simple really:

  • Local variables are stored on the stack.
  • Member variables are stored within the object, wherever that is stored.
  • Value type objects are stored in the variable, wherever that is stored.
  • Reference type objects are stored on the heap and a reference to them is stored in the variable, wherever that is stored.

CodePudding user response:

A struct is always value type. If you have an array of structs, the array will be a reference type but contain value types. If the struct contains arrays then the struct will be a value type that contains reference types.

In your multi-threading example, you would be creating a copy of the struct so would be able to change it on the other thread without worrying about the original being updated.

  • Related