Enum 的 C# 显式初始化

发布于 2024-10-07 01:07:48 字数 1917 浏览 1 评论 0原文

我有以下声明:

private Enum _statusValue;

我真正想说的是:

private Enum _statusValue = 0;

尽管我知道这是多余的。只是我喜欢明确指定初始化。

但这是不允许的。

有没有一些简单的方法来指定这种声明的初始化?

编辑-让我再试一次。

这是我正在做的事情的一个人为/简化的示例。

using System;

namespace EnumTesting
{
   public enum EFixedPhoneUsability
   {
      UnknownValue,       // Should not occur?
      IsUsable,           // User is near the telephone
      NotUsable,          // User not near the telephone
      BeingUsed,          // Busy
      DoNotDisturb,       // This is phone physical state, not user attitude
      CallsForwarded      // This is phone physical state
   }

   public enum EMobilePhoneConnectivity
   {
      UnknownValue,       // Should not occur?
      OnNetIdle,          // I.e., usable
      OnNetBusy,          // Being used
      NoConnection        // Turned off, in elevator or tunnel or far from civilization
   }

   public class Program
   {
      public Enum StatusValue;

      static void Main(string[] args)
      {
         Program p = new Program();

         p.StatusValue = EMobilePhoneConnectivity.NoConnection;

         int i = (int) (EMobilePhoneConnectivity) p.StatusValue;

         p.StatusValue = EFixedPhoneUsability.DoNotDisturb;

         i = (int) (EFixedPhoneUsability) p.StatusValue;
      }
   }
}

所以我的问题是,我可以向此语句添加通用初始值设定项吗?

      public Enum StatusValue;

第二次编辑:

没关系,由于这篇文章,我发现了我的方法的错误:

如何从 System.Enum 转换为基本整数?

让我意识到我做错了什么的关键短语是:“枚举是值类型(内部仅由整数表示)常量),而 System.Enum 是引用类型”。

所以我不想这样说:

private Enum _statusValue = 0;

我想说的是,这是完全正确的:

private Enum _statusValue = null;

感谢那些试图提供帮助的人。

I have the following statement:

private Enum _statusValue;

What I'd really like to say is:

private Enum _statusValue = 0;

even though I know it's redundant. It's just that I like to explicitly specify the initialization.

But that is not allowed.

Is there some simple way of specifying the initialization on this kind of declaration?

EDIT - Let me try again.

Here is a contrived / simplified example of what I'm doing.

using System;

namespace EnumTesting
{
   public enum EFixedPhoneUsability
   {
      UnknownValue,       // Should not occur?
      IsUsable,           // User is near the telephone
      NotUsable,          // User not near the telephone
      BeingUsed,          // Busy
      DoNotDisturb,       // This is phone physical state, not user attitude
      CallsForwarded      // This is phone physical state
   }

   public enum EMobilePhoneConnectivity
   {
      UnknownValue,       // Should not occur?
      OnNetIdle,          // I.e., usable
      OnNetBusy,          // Being used
      NoConnection        // Turned off, in elevator or tunnel or far from civilization
   }

   public class Program
   {
      public Enum StatusValue;

      static void Main(string[] args)
      {
         Program p = new Program();

         p.StatusValue = EMobilePhoneConnectivity.NoConnection;

         int i = (int) (EMobilePhoneConnectivity) p.StatusValue;

         p.StatusValue = EFixedPhoneUsability.DoNotDisturb;

         i = (int) (EFixedPhoneUsability) p.StatusValue;
      }
   }
}

So my question is, can I add a generic initializer to this statement?

      public Enum StatusValue;

SECOND EDIT:

Never mind, I have discovered the error of my ways, thanks to this posting:

How to convert from System.Enum to base integer?

The key phrase, which made me realize what I was doing wrong, is this: "enumerations are value types (and internally represented only by integer constants) while System.Enum is a reference type".

So I do not want to say this:

private Enum _statusValue = 0;

I want to say this, which is perfectly valid:

private Enum _statusValue = null;

Thank you to those who tried to help.

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

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

发布评论

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

评论(2

烟凡古楼 2024-10-14 01:07:48

您始终可以稍微不同地声明您的枚举并添加默认状态(实际上是建议的Microsoft):

public enum MyEnum
{
    Default = 0,
    One = 1, 
    Two = 2
}

或者简单地(自动编号从 0 开始):

public enum MyEnum
{
    Default,
    One,
    Two
}

这将允许您执行以下操作:

private MyEnum _enum = MyEnum.Default;

You could always declare your Enum slightly differently and add a default state (which is actually suggested by Microsoft):

public enum MyEnum
{
    Default = 0,
    One = 1, 
    Two = 2
}

Or simply (automatic numbering starts at 0):

public enum MyEnum
{
    Default,
    One,
    Two
}

Which would allow you to do the following:

private MyEnum _enum = MyEnum.Default;
若水微香 2024-10-14 01:07:48

我的问题基本上是错误的。我错误地认为 Enum(如枚举)是一种值类型,但它实际上是一种引用类型。

所以我不想这样说:

private Enum _statusValue = 0; 

我想说的是,这是完全正确的:

private Enum _statusValue = null; 

感谢那些试图提供帮助的人。

My question was basically in error. I had mistakenly thought Enum (like enums) was a value type, but it is actually a reference type.

So I do not want to say this:

private Enum _statusValue = 0; 

I want to say this, which is perfectly valid:

private Enum _statusValue = null; 

Thank you to those who tried to help.

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