enum类型
enum (C# Reference)
The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.
Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. However, an enum can also be nested within a class or struct.
By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.
For example, in the following enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth.
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
Enumerators can use initializers to override the default values, as shown in the following example.
enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
In this enumeration, the sequence of elements is forced to start from 1 instead of 0. However, including a constant that has the value of 0 is recommended.
For more information, see Enumeration Types (C# Programming Guide).
Every enumeration type has an underlying type, which can be any integral type except char.
The default underlying type of enumeration elements is int.
To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type, as shown in the following example.
enum Days : byte {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.
A variable of type Days can be assigned any value in the range of the underlying type; the values are not limited to the named constants.
The default value of an enum E is the value produced by the expression (E)0.
Note:
An enumerator cannot contain white space in its name.
The underlying type specifies how much storage is allocated for each enumerator.
However, an explicit cast is necessary to convert from enum type to an integral type.
For example, the following statement assigns the enumerator Sun to a variable of the type int by using a cast to convert from enum to int.
int x = (int)Days.Sun;
When you apply System.FlagsAttribute to an enumeration that contains elements that can be combined with a bitwise OR operation,
the attribute affects the behavior of the enum when it is used with some tools.
You can notice these changes when you use tools such as the Console class methods and the Expression Evaluator. (See the third example.)
Enumeration Types (C# Programming Guide)
enum MyType { A, B }
MyType myType = MyType.A; Console.WriteLine(myType); int a = (int) myType; Console.WriteLine(a); int b = 3; myType = (MyType) b; Console.WriteLine(myType);
上面的例子,MyType中A默认是0,B递增1;
b=3;是无法转换为MyType的,转换不会出错,打印myType,显示的是3
The default value of enum
https://stackoverflow.com/questions/4967656/what-is-the-default-value-for-enum-variable
It is whatever member of the enumeration represents the value 0
. Specifically, from the documentation:
The default value of an
enum E
is the value produced by the expression(E)0
.
As an example, take the following enum:
enum E
{
Foo, Bar, Baz, Quux
}
Without overriding the default values, printing default(E)
returns Foo
since it's the first-occurring element.
However, it is not always the case that 0
of an enum is represented by the first member. For example, if you do this:
enum F
{
// Give each element a custom value
Foo = 1, Bar = 2, Baz = 3, Quux = 0
}
Printing default(F)
will give you Quux
, not Foo
.
If none of the elements in an enum G
correspond to 0
:
enum G
{
Foo = 1, Bar = 2, Baz = 3, Quux = 4
}
default(G)
returns literally 0
, although its type remains as G
(as quoted by the docs above, a cast to the given enum type).
var array = Enum.GetValues(typeof(DayOfWeek));
foreach (int v in array)
{
Console.WriteLine(v);
}
foreach (var item in array)
{
Console.WriteLine(item);
}
The first loop in your code outputs the integer values of the enumeration constants, while the second loop outputs the names of the enumeration constants.
Here's an example output for the two loops:
0
1
2
3
4
5
6
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
The int
values correspond to the underlying value of each enumeration constant, which defaults to starting at 0 and incrementing by 1 for each constant. However, this can be customized by explicitly assigning integer values to each constant using the = value
syntax.
In the first foreach loop, the variable v
is explicitly declared as an int
, so only the integer value of each enumeration constant is printed.
In the second foreach loop, the var
keyword is used, so the type of the variable item
is inferred by the compiler to be the underlying type of the DayOfWeek
enumeration, which is int
. However, the ToString
method is automatically called on each enumeration constant, so the name of the constant is printed instead of its integer value.
Enum.GetValues(typeof(DayOfWeek))
returns an array of DayOfWeek
enum values of type System.Array
. The foreach
loop iterates over each element in the array and stores the value in the variable v
, which is of type int
since DayOfWeek
is an enum with an underlying type of int
. The loop then writes each v
value to the console.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2015-04-27 父类和子类的构造函数