2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[C#] enumの最大値を取ろうとした話

2
Last updated at Posted at 2026-01-02

enumの最大値を取得したいとき、
最後にMax,等を付けるとUnityのInspectorに表示されてしまうので

    public enum PlayerSkinType
    {
        None = -1,
        Doctor = 0,
        Farmer = 1,
        Boy = 2,
        Girl = 3,
        Rooster = 4,
        ClappyRed = 5,
        ClappyGreen = 6,
        ClappyYellow = 7,
        ClappyBlue = 8,
        Scientist = 9,
    }

の最大値(一番最後に指定したものが最大値と想定)を取得するために

private static readonly int[] PlayerSkinTypeValues = System.Enum.GetValues(typeof(PlayerSkinType)).Cast<int>().ToArray();

としてみたのですが、
(int)(PlayerSkinTypeValues[PlayerSkinTypeValues.Length -1])が最大値になりませんでした。

image.png
必ずしも定義順にはならないんですね。
なので、もうひと手間かけて最大値をとる必要があります。

using System.Linq;
int max = PlayerSkinTypeValues.Max();
2
2
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?