C# 和类中的嵌套枚举

发布于 2024-12-11 21:25:53 字数 159 浏览 0 评论 0原文

我听说 C# 中不可能嵌套 enum 。那么如何将以下结构转换为类层次结构或其他结构。所以我希望该类充当 enum

在此处输入图像描述

I heard that nesting of enum is not possible in C#. Then how can convert the following structure to a class hierarchy or something else. So I want the class to act as an enum

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花开半夏魅人心 2024-12-18 21:25:53

嵌套类和 const 字段

class Cat1
{
    public const int Public = 1;
    public class Private
    {
        public const int Abc = 2;
        public const int Mno = 3;
        public const int Pqr = 4;
    }
}

nested classes and const fields

class Cat1
{
    public const int Public = 1;
    public class Private
    {
        public const int Abc = 2;
        public const int Mno = 3;
        public const int Pqr = 4;
    }
}
权谋诡计 2024-12-18 21:25:53
public class Cat1
{
  public enum Publicpart
  {
     Xyz
  }

  private enum Privatepart
  {
     Abc, Mno, Pqr
  }
}

那么你可以这样称呼它

Cat1.Publicpart.Xyz

,或者如果你有私人访问权限

Cat1.Privatepart.Abc
public class Cat1
{
  public enum Publicpart
  {
     Xyz
  }

  private enum Privatepart
  {
     Abc, Mno, Pqr
  }
}

then you can call it like this

Cat1.Publicpart.Xyz

or if you have private acces

Cat1.Privatepart.Abc
情绪操控生活 2024-12-18 21:25:53

您可以使用hirerchy作为类结构,每个类都有自己的枚举属性

you can use the hirerchy as class structure , which every class has a property of its own of enum

迷迭香的记忆 2024-12-18 21:25:53

如果您想通过枚举解决此问题,您应该重新考虑,因为第一个枚举类别对我来说代表某种“可见性”概念,而第二个类别仅对具有“公共”可见性的实例有效。

用这样的方法解决你的问题怎么样:

public enum Visibility
{
    Public,
    Private
}

public abstract class VisibilityState
{
    public Visibility Visibility { get; private set; }

    protected VisibilityState(Visibility visibility)
    {
        Visibility = visibility;
    }
}

public class PublicVisibilityState : VisibilityState
{
    public PublicVisibilityState() : base(Visibility.Public) { }
}

public class PrivateVisibilityState : VisibilityState
{
    public PrivateVisibilityState() : base(Visibility.Private) { }
    public OtherEnum OtherEnumState { get; set; }
}

public enum OtherEnum
{
    Abc, Mno, Pqr
}

You should rethink if you want to solve this problems via enums because the first enum category represents to me some kind of "visibility" concept while the second category is only valid of instances with visibility "public".

What about solving your issue with something like this:

public enum Visibility
{
    Public,
    Private
}

public abstract class VisibilityState
{
    public Visibility Visibility { get; private set; }

    protected VisibilityState(Visibility visibility)
    {
        Visibility = visibility;
    }
}

public class PublicVisibilityState : VisibilityState
{
    public PublicVisibilityState() : base(Visibility.Public) { }
}

public class PrivateVisibilityState : VisibilityState
{
    public PrivateVisibilityState() : base(Visibility.Private) { }
    public OtherEnum OtherEnumState { get; set; }
}

public enum OtherEnum
{
    Abc, Mno, Pqr
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文