c#枚举类型_C枚举类型

c#枚举类型

Using the typedef and enum keywords we can define a type that can have either one value or another.

使用typedefenum关键字,我们可以定义一个可以具有一个值或另一个值的类型。

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 bool type, so this example is not really practical, but you get the idea.

C带有bool类型 ,因此该示例并不实际,但您可以理解。

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:

这是使用此枚举类型的简单程序:

#include <stdio.h>

typedef 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) ,但是对于我们人类来说,使用名称而不是数字来进行推理更简单,因此这是一种非常方便的语法。

翻译自: https://flaviocopes.com/c-enums/

c#枚举类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值