Store different structs in a list?
I have a world that have different types of items, each of these items have data and each type of item has his own struct, i am trying to loop through all the items in a world, but i dont understand how can i store different types of struct in a list.
like i cant add door struct in a window struct list, and if i make
List<object>
I would defeat the whole point of using struct to maintain efficiency as this would be stored on the heap16 Replies
struct
is not a magical way to save allocationsMatt Watson
Stackify
Why Premature Optimization Is the Root of All Evil
The performance and scalability of your application are important. Understand what to avoid and what to prioritize when building your app.
if you are storing structs in a list, you already have them in the heap
and you don't get to use free things like inheritance
while being very limited in terms of what you do
i'll let Aoba cook
also. structs shouldn't generally be used if it's bigger than something like 16 bytes; being by-val means the CPU has to do more work copying the whole struct around if it has to be assigned to a variable / passed as a parameter
lol
i feel dumb
do you recommend using
List<object>
?
cant i use ref keyword?nah, not raw objects
try coming up with a way to figure out what kind of things are shared and could be represented with inheritance or something
or a dictionary or something
how to store such a json object?
should i use classes then?
it's hard to tell with just that. But for me it's too late to take a look at the whole thing >.>
Don't use structs unless you know what you are doing
classes are fine
unless we're working with absurdly large data sets where these kinds of optimizations would start mattering, not even worth the thought
in my opinion
ref is something you use to work around the whole copying problem, but I believe it still is a wrong approach to be used here
if i use classes i still end up with the same problem
i still have to put them in a list
at least you now have inheritances
should i create an interface?
define a base item type that has all the properties all items have
names, ids, that kind of stuffs
then if there are properties that only certain types have then you can define properties for that in a derived type
....obviously there are times where this isn't a viable solution because the structures are very dynamic, but it is still way better than having an
object
that you'll need to write thousands of if-else / switch statements forah ig i will use this approach
thanks 👍