How can I create an enum using numbers?

前端 未结 7 1337
野趣味
野趣味 2021-01-11 11:11

Is it possible to make an enum using just numbers in C#? In my program I have a variable, Gain, that can only be set to 1, 2, 4, and 8. I am using a propertygrid control to

相关标签:
7条回答
  • 2021-01-11 11:56

    There have been a good amount of answers on this already however I will offer another solution. If you want your control to show an actual number as opposed to the english word for the number in your control you can use data annotations.

    using System.ComponentModel.DataAnnotations;
    
    private enum GainValues 
    {
     [Display(Name = "1")]
     One,
     [Display(Name = "2")]
     Two,
     [Display(Name = "4")] 
     Four, 
     [Display(Name = "8")]
     Eight
    }
    

    This is useful anywhere in your program where you want to use a friendly name as opposed to a member's actual name.

    0 讨论(0)
提交回复
热议问题