Emz
Emz

Reputation: 1280

Class vs Struct

Using these two as references

Immutable class vs struct

http://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx

I wonder what is recommended in my case (and preferably why). I am currently building an inventory manager, add, delete, move, remove Item. (Will later have equip/unequip as well, but they do not directly integrate with my inventory, as the inventory will only manage the remove (for equip) and add (for unequip)

So either a class or a struct, that is the question!

One person points out a 8 byte limit, msdn says 16. By Item class will hold a lot more than that.

private string _name;
private string _desc;

private bool _dropable;
private int _price;
private int _maxQuantity;

My ItemSlot struct/class will have an Item (or an int _itemId) as well as a quantity. Which should (yes?) add up to well over 16 bytes.

private ItemSlot[] _inventory;

public const int INVENTORY_SIZE = 50;

public Party () {
    ItemSlot[] _inventory = new ItemSlot[INVENTORY_SIZE];
}

The tutorial series I am trying to follow uses a struct, however; now with this knowledge am I correct in that I should have a class for the item slots? Or is my understanding all to shallow?

Upvotes: 2

Views: 294

Answers (2)

Christian Sauer
Christian Sauer

Reputation: 10899

As others mentioned: Don't worry about memory usage so much. Or rather: Worry about memory usage where it matters. But what really matters: A class is flexible are struct is not. It you need to add some logic to your data later on, this is possible with a class, but not with a struct. E.g. a class can have a method, but not a struct.

This can be a huge headache, I have often thought "damn now I have to provide a method which does some task and now I have to change this struct to class". So my rule of thumb is:

Use a struct only when: There is no foreseeable need for methods or complex getters / setters AND the data are very small and are unlikely to grow. The "and clause" comes from the fact that complex structures are getting a method in the future, regardless what you are thinking now.

If you look into the net framework, classes are used almost everywhere, where structs are only used for very small related data like a Point (x and y coordinates)

Upvotes: 1

Simon Whitehead
Simon Whitehead

Reputation: 65079

Go with a class.

The recommended sizes for structs isn't because of available memory (yes, memory is cheap these days as pointed out by Arun in the comments).. but the real reason is because structs are copied by value. This means that every time you pass your structure around.. every field is copied along with it. So, the "struct byte-limit" recommendations you're seeing everywhere is to avoid that.

A class on the other hand, only copies the value of a reference.. which is the native word size of the processor it is running on .. making the copy operation barely measurable.

You stated your structure is going to be much bigger than 16 bytes.. that is definitely reason enough to go with a class purely to avoid the overhead of copying around entire blocks of memory when using a struct.

Upvotes: 6

Related Questions