Enums are a way to create a somewhat-strongly-typed set of aliases for integers. However, you can always cast an integer (of whatever type you specified for your enum, by default int
) to the enum type, so you have to trust the code to not do that (which might be acceptable for an internal enum type). Additionally, you can't add any additional information to the enum value itself; any time you interpret the value, you have to use a switch
or similar construct.
If you want to actually have a class with properties and such, but want to restrict the number of instances of that class, you can extend the singleton pattern like this:
sealed class Player
{
// Properties that a Player object has, an improvement over using an enum
// which don't allow you to specify properties.
public int Number { get; }
public bool IsHost { get; }
// This constructor is private, so only the code inside the Player class may create a Player object
// (notwithstanding reflection, etc., which are outside the rules of static typing).
private Player(int number, bool isHost)
{
Number = number;
IsHost = isHost;
}
// Static properties provide singleton instances of Player for each player number.
public static Player One { get; } = new Player(1, true);
public static Player Two { get; } = new Player(2, false);
public static Player Three { get; } = new Player(3, false);
public static Player Four { get; } = new Player(4, false);
}
And then use it like this:
void Main()
{
SomethingThatTakesPlayer(Player.Three);
}
void SomethingThatTakesPlayer(Player p)
{
Console.WriteLine($"Player #{p.Number} is the host? {p.IsHost}");
}