Despite the fact that java seems to have this feature which C# doesn't, the reality is (as always) that C# is a much more modern language, and it requires much less code to produce the same results.
Your 72 lines of java code can be translated into these 35 lines of C#:
public class Direction
{
public static readonly Direction N = new Direction(0, 1);
public static readonly Direction S = new Direction(0, -1);
public static readonly Direction E = new Direction(1, 0);
public static readonly Direction W = new Direction(-1, 0);
private Direction(int stepSizeX, int stepSizeY)
{
this.StepSizeForXAxis = stepSizeX;
this.StepSizeForYAxis = stepSizeY;
}
static Direction()
{
N.Left = W;
N.Right = E;
S.Left = E;
S.Right = W;
E.Left = N;
E.Right = S;
W.Left = S;
W.Right = N;
}
public Direction Left { get; private set; }
public Direction Right { get; private set; }
public int StepSizeForXAxis { get; private set; }
public int StepSizeForYAxis { get; private set; }
}
This results in a class that can only be instantiated by itself (because of the private constructor), and has members that you can use in the same way you would use a C# enum
, with the advantage of the additional properties:
var south = Direction.S;
var east = south.Left;
Console.WriteLine(east == south); // True
Console.WriteLine(south.StepSizeForXAxis); //0
Console.WriteLine(south.StepSizeForYAxis); //-1
java can be hardly compared to C# anymore, let alone claim any advantage over it, at least at the language level.