let's assume you need to store a certain amount of data, which is represented either by a struct or a class. You'll only store a maximum of N elements, if adding more, you'd have to first delete an existing one.
You can use any way you like to store the data - a List, an array (or anything else if you'd like).
In terms of Garbage Collection - does it make a difference whether you use a class or struct to represent the information? Does it make a difference whether you store it in a List or Array?
My intuition is the best way would be using an array of structs, as in principle a GC shouldn't ever need to collect anything here (while the array is stored on the heap, its size remains constant and so no collection ever takes place I believe?).
I'm not entirely sure how a list of structs would be represented - is there any form of garbage collection associated with adding/deleting struct elements?
As for using classes instead of structs, I'd expect once removed from the list, or overridden in the array, the old reference would need to be collected, so either would put on GC pressure.
Would appreciate input if my intuition is correct here or if I'm going wrong anywhere! Thanks
Example code:
public struct SStruct
{
int ABC;
}
public class SClass
{
int ABC;
}
public class Test
{
List<SStruct> _data1;
List<SClass> _data2;
SStruct[100] _data3;
SClass[100] _data4;
public void run()
{
var sStruct = new SStruct();
var sClass = new SClass();
if(data1.Count > 0)
_data1.RemoveAt(0);
if(data2.Count > 0)
_data2.RemoveAt(0);
_data1.Add(sStruct);
_data2.Add(sClass);
_data3[0] = sStruct;
_data4[0] = sClass;
}