2

So, I had this bright idea. I need to loop though all directions in a flathead hex. So, given the enum

public enum HexDirectionFlat
{
    UP, UP_LEFT, DOWN_LEFT, DOWN, DOWN_RIGHT, UP_RIGHT
}

Is then this a valid for-loop?

for(HexDirectionFlat dir = HexDirectionFlat.UP; dir <= HexDirectionFlat.UP_RIGHT; dir++)

If so, are there any performence reasons not to use this?

Eugene Sh.
  • 16,386
  • 4
  • 33
  • 51
Lars Erik Grambo
  • 301
  • 2
  • 11
  • Yes you can but it is probably slower than directly using the int values. – xanatos Jun 04 '18 at 13:59
  • `public enum HexDirectionFlat : int` to specify the underlying type's width explicitly. You'd probably want to `foreach (HexDirectionFlat dir in Enum.GetValues(typeof(HexDirectionFlat))` though for clarity's sake. – Patrick Roberts Jun 04 '18 at 14:03
  • Just adding, performance aside, `GetValues` avoids the potential for breaking your loop should the enum’s values ever change (less fragile). – blins Jun 04 '18 at 14:18
  • *"Is then this a valid for-loop?"* -- Why are you asking us? Try it. Compile it and run it. – 15ee8f99-57ff-4f92-890c-b56153 Jun 04 '18 at 14:18

1 Answers1

4

Is then this a valid for-loop?

Yes, absolutely.

If so, are there any performance reasons not to use this?

No, this is a very efficient way of iterating over enum values - likely to be more efficient than calling Enum.GetValues which needs to allocate an array, for example.

The disadvantages are:

  • You need to know the "lowest" value of your enum (so adding a new lower value would involve changing every loop like this)
  • You need to know the "highest" value of your enum (so adding a new lower value would involve changing every loop like this)
  • If your enum has "gaps" (e.g. values 0, 1, 3, 4, 5 - but no 2) your loop won't skip over those gaps
  • If your enum goes right up to the highest value of the underlying type (e.g. 255 for a byte-based enum) the loop will never terminate

You could use constants to mitigate the first two points; the third point would only be relevant with explicitly-valued enums; the last point would be pretty rare. You could write unit tests to check for all of them.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • @EugeneSh.: Well that will change the iteration order, sure - but calling `Enum.GetValues` would have the same issue. That's only an error if you don't *want* a change in values to incur a change in iteration order. – Jon Skeet Jun 04 '18 at 14:05
  • ... Sorry I did not notice you put it under "disadvantages" section – Eugene Sh. Jun 04 '18 at 14:05