本文共 1557 字,大约阅读时间需要 5 分钟。
c#枚举类型
Using the typedef
and enum
keywords we can define a type that can have either one value or another.
使用typedef
和enum
关键字,我们可以定义一个可以具有一个值或另一个值的类型。
It’s one of the most important uses of the typedef
keyword.
这是typedef
关键字最重要的用途之一。
This is the syntax of an enumerated type:
这是枚举类型的语法:
typedef enum { //...values} TYPENAME;
The enumerated type we create is usually, by convention, uppercase.
按照惯例,我们创建的枚举类型通常是大写的。
Here is a simple example:
这是一个简单的示例:
typedef enum { true, false} BOOLEAN;
C comes with a , so this example is not really practical, but you get the idea.
C带有 ,因此该示例并不实际,但您可以理解。
Another example is to define weekdays:
另一个示例是定义工作日:
typedef enum { monday, tuesday, wednesday, thursday, friday, saturday, sunday} WEEKDAY;
Here’s a simple program that uses this enumerated type:
这是使用此枚举类型的简单程序:
#includetypedef enum { monday, tuesday, wednesday, thursday, friday, saturday, sunday} WEEKDAY;int main(void) { WEEKDAY day = monday; if (day == monday) { printf("It's monday!"); } else { printf("It's not monday"); }}
Every item in the enum definition is paired to an integer, internally. So in this example monday
is 0, tuesday
is 1 and so on.
枚举定义中的每个项目都在内部与一个整数配对。 因此,在此示例中, monday
为0, tuesday
为1,依此类推。
This means the conditional could have been if (day == 0)
instead of if (day == monday)
, but it’s way simpler for us humans to reason with names rather than numbers, so it’s a very convenient syntax.
这意味着条件可以是if (day == 0)
而不是if (day == monday)
,但是对于我们人类来说,使用名称而不是数字来进行推理更简单,因此这是一种非常方便的语法。
翻译自:
c#枚举类型
转载地址:http://pamgb.baihongyu.com/