You can use an enum and use it like this:
public enum Direction {
1 = Up,
2 = Right,
3 = Down,
4 = Left
}
public class UseDirection {
public Direction Dir { public get; private set; }
public void SetDirection(Direction dir) {
Dir = dir;
}
}
However, now if you want to do some manipulations on Directions, or do some calculation, you cannot implement it in Direction since it is an enum.
You could also make an abstract class Direction, and make the actual directions derive from Direction, like this:
public abstract class Direction {
private class DirectionUp : Direction { }
private class DirectionDown : Direction { }
private class DirectionLeft : Direction { }
private class DirectionRight : Direction { }
public static Direction Up => new DirectionUp();
public static Direction Down => new DirectionDown();
public static Direction Left => new DirectionLeft();
public static Direction Right => new DirectionRight();
}
Now you can use Direction
like this:
public class AggregatesDirection {
public Direction Dir { public get; private set; }
public AggregatesDirection() {
Dir = Direction.Up; //uses the static property in Direction that will use the private constructor of the private class in Direction.
}
}
This pattern is nice, because you can now just use the Direction
interface to do all the stuff relevant to direction, and still have different implementations for when you would, say, relocate something for a given direction in the different direction classes.
You could also just make the classes public and outside of the abstract Direction class, but you would expose the types to the namespace then, which is probably not necessary.