C# 枚举

在 C# 中,enum (enumeration 的缩写) 是一种用户定义的、具有一组固定相关值的类型。

我们使用 enum 关键字来创建枚举。例如:

enum Months
{
    may,
    june,
    july,
}

这里,

  • Months - 枚举名称
  • mayjunejuly - 枚举成员 (也称为字符串常量)

#define-an-enum 定义枚举

// define an enum named Weekdays 
enum Weekdays
{
    sunday,
    monday,
    tuesday,
}

在这里,我们定义了一个名为 Weekdays 的枚举。

访问枚举成员

我们使用枚举名称和 . 运算符来访问枚举成员。

从上面的 Weekdays 枚举中,

  • Weekdays.sunday - 访问枚举成员 sunday
  • Weekdays.monday - 访问枚举成员 monday

示例:C# 枚举

using System;

// define an enum 
enum Weekdays { sunday, monday, tuesday, }
class Program { static void Main() {
// assign sunday to meetingDay Weekdays meetingDay = Weekdays.sunday;
Console.WriteLine(meetingDay); } }

输出

sunday

在上面的示例中,我们定义了一个名为 Weekdays 的枚举。此外,我们将成员值 sunday 分配给了枚举变量 meetingDay,如下所示:

// assign sunday to meetingDay
Weekdays meetingDay = Weekdays.sunday;

C# 枚举值

在 C# 中,我们可以为枚举成员分配数字值。例如:

using System;

// define an enum and assign numeric values 
enum Season
{
    summer = 1,
    winter = 2,
    autumn = 3,
}

在这里,我们将数字值 123 分别分配给了成员 summerwinterautumn


枚举转换

要打印枚举值,我们需要使用 显式类型转换 将枚举成员转换为其相应的值。

让我们看下面的例子:

using System;

//  an enum that contains shapes present in deck of card 
enum Cards
{
    Diamond = 1,
    Spade = 2,
    Club = 3,
    Heart = 4,
}

class Program
{
    static void Main()
    {
        // type casting
// convert string value "Spade" to integer value int myCard = (int)Cards.Spade;
Console.WriteLine("Integer value of string constant is: " + myCard); } }

输出

Integer value of string constant is: 2

在这里,我们将字符串值 "Spade" 转换为其对应的整数值 2


枚举默认值

如果我们没有为枚举成员分配任何值,默认情况下,第一个成员将被分配 0。然后其他成员的值会增加 1。例如:

using System;

//  an enum that contains names of planet  
enum Planet
{
// value is 0 mercury, // value is 1 venus, // value is 2 earth,
} class Program { static void Main() { // type casting enum to int int planet1 = (int)Planet.mercury; int planet2 = (int)Planet.venus; int planet3 = (int)Planet.earth; Console.WriteLine("Value of first member: " + planet1); Console.WriteLine("Value of second member: " + planet2); Console.WriteLine("Value of third member: " + planet3); } }

输出

Value of first member: 0
Value of second member: 1
Value of third member: 2

在上面的示例中,我们使用类型转换将枚举成员转换为其对应的数字值。这里:

  • Mercury - 0 (第一个成员)
  • Venus - 1 (第二个成员)
  • Earth - 2 (第三个成员)

注意: 我们可以为枚举成员分配不同的值。例如:

enum Planets
{
    mercury = 4,
    venus = 2,
    earth = 7,
}

指定枚举类型

在枚举中,我们分配给成员的数字值可以是任何整型数据类型,如 byteintshortlongushort 等。

要指定数据类型,我们在枚举名称后使用 : typeName。例如:

using System;
enum Holidays : long
{
    christmas = 123,
    thanksgiving = 124,
    halloween = 125,
}

在上面的示例中,我们将枚举值的数据类型指定为 enum Holidays : long

注意: 默认情况下,枚举成员的类型为 int。


常见问题

我们可以直接在类中定义枚举吗?

是的,我们可以将 enum 直接定义在类中。例如:

using System;
class Program { enum Season { spring, summer, }
static void Main() { Console.WriteLine(Season.spring); } }

输出

spring

在这里,我们将一个名为 Season 的枚举直接定义在了 Program 类中。

注意: 我们也可以直接在命名空间或结构中定义枚举。

为什么要使用枚举?

我们使用枚举的原因如下:

替换 int 常量的使用

我们使用枚举来代替 int 常量。假设我们定义了 int 常量。

const int small = 0;
const int medium = 1;
const int large = 2;

Console.WriteLine(small);
// Output: 0

这里,如果我们打印常量值,就会出现问题。因为当我们打印常量 small 时,会打印值 0,这没有帮助,因为很难知道 0 指的是什么。

所以,我们可以简单地使用枚举来代替 int 常量。例如:

enum Size
{
    small, medium, large,
}

Console.WriteLine(Size.small);
// Output: small

在这里,当我们打印枚举值时,我们会得到 small 作为输出,这比 0 更易于理解。这使得我们的代码更直观。

编译时类型安全

枚举提供编译时安全性。假设我们声明一个 Size 类型的变量。例如:

enum Size
{
    small, medium, large,
}

Size size;

在这里,可以保证该变量将保存这三个枚举值之一。现在,如果我们尝试传递除这四个值之外的其他值,编译器将生成一个错误。

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战