if my child class holds only values that is getting access in another class should i make the child class a struct

if my child holds only values that is getting access in another class should i make the child class struct or a class
would it hurt or help performance?.
what the code might look like

skills{//child class
string skillname;
string skilldesc;
int stateffect1;
int stateffect2;
int stateffect3;
texture2d image;
int something[];
}

mainclass{
skills[] listofskills;
}

1 Answer

1

Firstly, be careful with the word “baseclass” because it means a class that another class is extending. Looking at your pseudo code it doesn’t look like that’s what you are doing

The main benefit of a struct is that it’s easy on the garbage collector.
The main disadvantage of it is that whenever you assign it or pass it as a parameter, it gets “copied”, which (depending on the “weight” of the struct) can get heavier than passing/assigning a class.

I’d say those are the most important points to consider.

If skills is something that you have a lot of or you create them constantly in the Update method or in loops etc. then using a struct might be good.

If skills is something that is stored for a longer time and passed around in your code, Class is a better option.

Looking at the variable names and imagining what skills could be used for, I’d probably make it Class.

Quoting from http://msdn.microsoft.com/

√ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

X AVOID defining a struct unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (int, double, etc.)
  • It has an instance size under 16 bytes
  • It is immutable.
  • It will not have to be boxed frequently.

In all other cases, you should define your types as classes.

ill go rename the word base class as a child class and use a "class" instead of a "struct" for my child class i guess if my child class is going to be modified ones but get multiple times then a class is more appropriate right? the class child class is created ones and a scene which uses it will live at least minutes i'm sure by short lived you mean less that a second life time plus texture2d isn't a primitive so that's another "nail in the coffin" and its a reference type now i'm curious to which part of a game you used a struct in thanks a bunch !

Yeah. Here short lived means a few seconds at most. For some reference for where and when to use a struct you could look a some readily made C# structs, like Vector3. It is used in all kinds of 3D graphics and physics calculations that happen thousands of times every second. It only holds a few float values and you could even say it represents "just a value" just like int, float etc. value types.