Enum
values are converted to values of the underlying type at compile time. Therefore, a variable of an enumeration type itself based on int
type (which is the default) doesn't use any more memory than any other int
variable in your code.
An Enum
underlying structure can be any of the following:
byte
sbyte
short
ushort
int
uint
long
ulong
Since your Player
enumeration type does not specify the underlying time, the compiler will see it as an int
-based Enum
. If memory is a concern, you may want to consider declaring it as derived from byte
instead:
enum Player : byte
{
First,
Second,
Third,
Fourth
};
Be careful though: a newly declared Enum
variable will have the same value as it's underlying type's default value, which will always be zero. In an enum with unspecified literal values, the first item in the list is assumed as the default value. In your case, that would be First
:
private Player _owner; // this value will initially equals Player.First
You may want to insert an additional Player
literal labelled None
, Empty
or Undefined
to represent a Player
variable default value:
enum Player : byte
{
Undefined = 0;
First,
Second,
Third,
Fourth
};
private Player _owner; // this value will initially equals Player.Undefined
Of course, if you are okay having First
as the default value, you may leave it as is. Be aware though that not having a dedicated default Enum
value is often considered a bad coding practice.
As an alternative, since any Enum
are structure-based, you may also declare your _owner
variable as Player?
so that it will instead be null
by default:
private Player? _owner; // this value will initially equals null
To sum it up, simply remember that Enum
literals simply acts as constants for their underlying type. Their purpose is to make code easier to read and to enforce a limited set of possible values at compile time.
For more information, look at the documentation.