【问题标题】:How to set internal Enum for particular value?如何为特定值设置内部枚举?
【发布时间】:2018-05-29 07:18:40
【问题描述】:

我想将enum 设为public,但enum 值之一应该是internal。这在 C# 中可行吗?

public enum Numbers
{
      One,
      Two,
      Three,
      internal Zero // I want zero as internal
}

【问题讨论】:

  • 外部代码读取值Numbers.Zero会发生什么?
  • @MichaWiedenmann Numbers.Zero 不应在命名空间外访问
  • 但这意味着什么?假设你有一个字段class Foo { public Numbers numbers = Numbers.Zero; } 如果有人读到Foo.numbers 会发生什么?
  • 简而言之:不,那是不可能的。然而,我怀疑让您的枚举成员成为您的实际问题的解决方案。那么,您认为这是解决方案的实际问题是什么?

标签: c# enums access-modifiers


【解决方案1】:

我认为您可以使用以下代码实现此目的:

public static class Numbers
{
    public static readonly int One = 1;
    public static readonly int Two = 2;
    public static readonly int Three = 3;
    internal static readonly int Zero = 0;
}

此外,它可以完全像enum一样使用。

【讨论】:

    猜你喜欢
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    相关资源
    最近更新 更多