Enum 的 C# 显式初始化
我有以下声明:
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 是引用类型”。
所以我不想这样说:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您始终可以稍微不同地声明您的枚举并添加默认状态(实际上是建议的Microsoft):
或者简单地(自动编号从 0 开始):
这将允许您执行以下操作:
You could always declare your Enum slightly differently and add a default state (which is actually suggested by Microsoft):
Or simply (automatic numbering starts at 0):
Which would allow you to do the following:
我的问题基本上是错误的。我错误地认为 Enum(如枚举)是一种值类型,但它实际上是一种引用类型。
所以我不想这样说:
我想说的是,这是完全正确的:
感谢那些试图提供帮助的人。
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:
I want to say this, which is perfectly valid:
Thank you to those who tried to help.