C# 如何判断枚举属性是否已设置?C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16554577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to tell if an enum property has been set? C#
提问by user2341534
I have a class with an enum property like so:
我有一个具有枚举属性的类,如下所示:
public class Foo
{
public Color ColorType {get;set;}
}
public enum Color
{
Red,
Green,
}
Now this class can be initialized like so:
现在这个类可以像这样初始化:
var foo = new Foo();
without the ColorType property ever being set. Now, I'm trying to create a method and perform actions on whether that enum was ever set or not, for example I have a method
没有设置 ColorType 属性。现在,我正在尝试创建一个方法并对是否设置过该枚举执行操作,例如我有一个方法
private void checkEnum(Foo foo)
{
if(foo.ColorType !=null)
{
//perform these actions
}else
{
//perform those actions
}
}
however I get a warning saying that value will never be null and upon further research, if the enum is never set if will default to the first value which would be Red in my case, I was thinking about adding a value to my enum which would be 'not set' and make that value the first one, so if it hasnt been set then the enum will have the value 'not set', is there a better way of doing this, my proposed method seems like it could get messy
但是我收到一条警告说值永远不会为空,并且经过进一步研究,如果从未设置枚举,如果将默认为第一个值,在我的情况下为红色,我正在考虑向我的枚举添加一个值设为“未设置”并将该值设为第一个,因此如果尚未设置,则枚举的值将变为“未设置”,是否有更好的方法来执行此操作,我提出的方法似乎可能会变得混乱
采纳答案by Michael Teper
You can use one of two methods: default enum value or a nullable enum.
您可以使用以下两种方法之一:默认枚举值或可为空的枚举。
Default enum value
默认枚举值
Since an enum is backed by an integer, and intdefaults to zero, the enum will always initialize by default to the value equivalent to zero. Unless you explicitly assign enum values, the first value will always be zero, second will be one, and so on.
由于枚举由整数支持,并且int默认为零,因此默认情况下枚举将始终初始化为等效于零的值。除非您明确分配枚举值,否则第一个值将始终为零,第二个值为 1,依此类推。
public enum Color
{
Undefined,
Red,
Green
}
// ...
Assert.IsTrue(Color.Undefined == 0); // success!
Nullable enum
可为空的枚举
The other way to handle unassigned enum is to use a nullable field.
处理未分配枚举的另一种方法是使用可为空的字段。
public class Foo
{
public Color? Color { get; set; }
}
// ...
var foo = new Foo();
Assert.IsNull(foo.Color); // success!
回答by ZZZ
enumis a value type so it cannot be null, and the storage is generally an integer. If you want a undefined value for your type, you may have
enum是值类型所以不能为空,存储一般是整数。如果你想要一个未定义的类型值,你可能有
public enum Color
{
Undefined,
Red,
Green,
}
回答by Dave K
As you've discovered, enumerations in C# are value types (they are essentially integers) not reference types so they will not default to NULL but rather the lowest integer value in the enumeration. Don't lose sight of this relationship when dealing with enums as it's one of their most useful attributes. Always remember that whether you explicitly state it or not,
正如您所发现的,C# 中的枚举是值类型(它们本质上是整数)而不是引用类型,因此它们不会默认为 NULL,而是枚举中最低的整数值。在处理枚举时不要忽视这种关系,因为它是它们最有用的属性之一。永远记住,无论你是否明确说明,
public enum Color
{
Red,
Green
}
equates to:
相当于:
public enum Color
{
Red = 0,
Green = 1
}
Though you may of course give each enumeration member any integer value that you like.
尽管您当然可以为每个枚举成员提供您喜欢的任何整数值。
As far as whether or not there is a better way of doing this, it really all depends on what "this" is, though there's nothing wrong with your suggestion of simply using the following enum setup:
至于是否有更好的方法来做到这一点,这实际上完全取决于“这个”是什么,尽管您建议简单地使用以下枚举设置并没有错:
public enum Color
{
None = 0,
Red,
Green
}
Generally you want to use enums when you have a decidedly finite and discrete set of possible values that you want to be able to select from by name. For example, say I have a method that takes one of the 4 cardinal directions (North, East, South, West) as a parameter. I decide that I want to number each of the directions in clockwise order, starting with 0 for North.
通常,当您希望能够通过名称从中选择一组绝对有限且离散的可能值时,您希望使用枚举。例如,假设我有一个方法,它将 4 个基本方向(北、东、南、西)之一作为参数。我决定按顺时针顺序对每个方向进行编号,从 0 开始表示北。
public enum Direction
{
North = 0,
East,
South,
West
}
Now, instead of having my function take an integer parameter and trusting that I'll remember what each number refers to, I can now have the function take an enumeration member as a parameter and know immediately which direction I'm dealing with. For example:
现在,不是让我的函数采用整数参数并相信我会记住每个数字所指的内容,我现在可以让函数采用枚举成员作为参数并立即知道我正在处理的方向。例如:
getNeighbor(1);
reads much easier as:
阅读起来更容易,因为:
getNeighbor(Direction.East);
回答by Sina Iravanian
Enums are Value Types, which means they are not references to an object stored somewhere else, and hence they cannot be null. They always have a default value just like intwhich will default to zero upon creation. I suggest two approaches:
枚举是值类型,这意味着它们不是对存储在其他地方的对象的引用,因此它们不能是null. 它们总是有一个默认值,就像int在创建时默认为零一样。我建议两种方法:
Add another enum entry called e.g.,
Nonewith value equal to zero. This way your enum value will default toNoneupon creation. Then you can checkif(foo.ColorType != Color.None).Make your
Colorproperty a nullable one like:public Color? ColorType { get; set; }. Now it will default tonulland can be assigned the valuenull. Read more aboutnullabletypes here: MSDN - Nullable Types (C#).
添加另一个名为 eg 的枚举条目,
None其值等于零。这样您的枚举值将None在创建时默认为。然后你可以检查if(foo.ColorType != Color.None)。让你的
Color财产为空的一个,如:public Color? ColorType { get; set; }。现在它将默认为null并且可以分配值null。nullable在此处阅读有关类型的更多信息:MSDN - Nullable Types (C#)。
回答by jstromwick
You have two real options. The first is to add an undefined value to enum. This will be the default value before the property is initialized.
你有两个真正的选择。第一个是向枚举添加一个未定义的值。这将是属性初始化之前的默认值。
1)
1)
public enum Color
{
Undefined,
Red,
Green,
}
With your check like:
用你的支票:
private void checkEnum(Foo foo)
{
if(foo.ColorType == Color.Undefined)
{
//perform these actions
}else
{
//perform those actions
}
}
2) Alternatively you can not add the undefined value and just make the property Nullable
2) 或者,您不能添加未定义的值,而只是创建属性 Nullable
public class Foo
{
public Color? ColorType {get;set;}
}
public enum Color
{
Red,
Green,
}
And perform your check like:
并执行您的检查,如:
private void checkEnum(Foo foo)
{
if(!foo.ColorType.HasValue)
{
//perform these actions
}else
{
//perform those actions
}
}
回答by Sellorio
You can make it so that the underlying private field is nullable, but the property is not.
您可以使基础私有字段可以为空,但属性不是。
E.g.
例如
class SomeClass
{
private Color? _color; // defaults to null
public Color Color
{
get { return _color ?? Color.Black; }
set { _color = value; }
}
public bool ColorChanged
{
get { return _color != null; }
}
}
That way if color == nullyou know it hasn't been set yet, and you are also stopping the user from setting it to null(or undefinedas other answers specify). If coloris nullyou have 100% certainty that the user has not set it ever.
这样,如果color == null您知道尚未设置它,并且您也阻止用户将其设置为null(或undefined其他答案指定的)。如果color是,null您 100% 确定用户从未设置过它。
Only catch is the default value returned by the getbut you could always throw an excepting if it better matches your program.
只有 catch 是由 返回的默认值,get但如果它更好地匹配您的程序,您总是可以抛出异常。
You can also take it one step further by making it so that the setonly sets the field if the given value is not equal to the default value (depending on your use case):
您还可以通过使其更进一步,以便set仅在给定值不等于默认值时设置字段(取决于您的用例):
public Color Color
{
get { return _color ?? Color.Black; }
set
{
if (value != Color)
{
_color = value;
}
}
}

