How can I cast int to enum?

后端 未结 30 2024
礼貌的吻别
礼貌的吻别 2020-11-22 00:56

How can an int be cast to an enum in C#?

30条回答
  •  Happy的楠姐
    2020-11-22 01:15

    Slightly getting away from the original question, but I found an answer to Stack Overflow question Get int value from enum useful. Create a static class with public const int properties, allowing you to easily collect together a bunch of related int constants, and then not have to cast them to int when using them.

    public static class Question
    {
        public static readonly int Role = 2;
        public static readonly int ProjectFunding = 3;
        public static readonly int TotalEmployee = 4;
        public static readonly int NumberOfServers = 5;
        public static readonly int TopBusinessConcern = 6;
    }
    

    Obviously, some of the enum type functionality will be lost, but for storing a bunch of database id constants, it seems like a pretty tidy solution.

提交回复
热议问题