Best structure for a growing collection of objects in C #?

For my Game Programming class, I create a class for the player character. The player can collect bonuses throughout the game, which are held and can be used whenever the player chooses.

I thought a dynamic array would work fine, but my background is in C ++ (we use Unity in a class that uses Java and C #), and I know that freeing up memory is handled differently in C # (I know that there is a garbage collector, but actually knows very little about how it functions). After browsing the website for a while, I could not find anything that seemed to correspond to the functionality I needed (or, if I did, it was above my head, and I did not realize it).

If someone could specify a C # structure or structures that would be good for storing a growing set of objects, that would be very helpful.

+4
source share
2 answers

A list is perhaps the easiest structure you could use, it is like a dynamic array that will automatically grow as things are added to it. It is strongly typed, so it will only contain objects of the same type (if you have an interface for your bonuses, you can have a list of IPowerUp instances)

+3
source

Start by browsing . Collections of general collections . (.NET generators are similar in concept to C ++ templates.) Either a List or Dictionary is likely to be useful to you depending on how you need to store and retrieve your objects.

Since you are likely to have object types, you can use a list dictionary where the key will be a string identifier or an enumeration of the types of collected objects, and the value will be a list of object instances.

0
source

Source: https://habr.com/ru/post/1379031/


All Articles