[英]How do I set an enum value inside another enum?
I want something like this. 我想要这样的东西。
public enum AgeGroup
{
Teenager = new enum { FromAge = 16, ToAge = 17 },
young = new enum { FromAge = 18, ToAge = 24 }
}
By this I want access it like AgeGroup.Teenager.FromDate
. 通过这个,我想像
AgeGroup.Teenager.FromDate
一样访问它。
A nested enum is not possible. 嵌套枚举是不可能的。
You can change your parent enum to a class, and it will work: 您可以将您的父枚举更改为一个类,它将起作用:
public class AgeGroup
{
public enum Teegaer
{
FromAge = 16, ToAge = 17
}
public enum young
{
FromAge = 18, ToAge = 24
}
}
An alternative approach is to create a static class : 另一种方法是创建一个静态类 :
public static class AgeGroup
{
public static class TeenAge
{
public static readonly int FromAge = 16;
public static readonly int ToAge = 17;
}
public static class YoungAge
{
public static readonly int FromAge = 18;
public static readonly int ToAge = 21;
}
}
Now you can refer to the variable, as below: 现在,您可以引用该变量,如下所示:
int teenAgeStarting = AgeGroup.TeenAge.FromAge //16
int youngAgeStarting = AgeGroup.YoungAge.FromAge //18
Implementation: DotNetFiddler 实施: DotNetFiddler
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.