C#枚举

C#枚举 首页 / C#入门教程 / C#枚举

C#中的枚举也称为枚举。它用于存储一组命名常量,如季节、天、月、大小等。枚举常量也称为枚举数。C#中的枚举可以在类和结构内部或外部声明。

枚举常量具有默认值,从0开始逐个递增。但是无涯教程可以更改默认值。

要记住的要点

  • 枚举有固定的常量集
  • 枚举提高了安全性
  • 枚举可以遍历

C#枚举示例

让无涯教程看一个简单的C#枚举示例。

using System;
public class EnumExample
{
    public enum Season { WINTER, SPRING, SUMMER, FALL }  

    public static void Main()
    {
        int x = (int)Season.WINTER;
        int y = (int)Season.SUMMER;
        Console.WriteLine("WINTER = {0}", x);
        Console.WriteLine("SUMMER = {0}", y);
    }
}

输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-enum.html

来源:LearnFk无涯教程网

WINTER = 0
SUMMER = 2

更改起始索引的C#枚举示例

using System;
public class EnumExample
{
    public enum Season { WINTER=10, SPRING, SUMMER, FALL }  

    public static void Main()
    {
        int x = (int)Season.WINTER;
        int y = (int)Season.SUMMER;
        Console.WriteLine("WINTER = {0}", x);
        Console.WriteLine("SUMMER = {0}", y);
    }
}

输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-enum.html

来源:LearnFk无涯教程网

WINTER = 10
SUMMER = 12

C#枚举天数示例

using System;
public class EnumExample
{
    public enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

    public static void Main()
    {
        int x = (int)Days.Sun;
        int y = (int)Days.Mon;
        int z = (int)Days.Sat;
        Console.WriteLine("Sun = {0}", x);
        Console.WriteLine("Mon = {0}", y);
        Console.WriteLine("Sat = {0}", z);
    }
}

输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-enum.html

来源:LearnFk无涯教程网

Sun = 0
Mon = 1
Sat = 6

C#枚举示例:使用getNames()遍历所有值

using System;
public class EnumExample
{
    public enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

    public static void Main()
    {
        foreach (string s in Enum.GetNames(typeof(Days)))
        {
            Console.WriteLine(s);
        }
    }
}

输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-enum.html

来源:LearnFk无涯教程网

Sun
Mon
Tue
Wed
Thu
Fri
Sat

C#枚举示例:使用getValues()遍历所有值

using System;
public class EnumExample
{
    public enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

    public static void Main()
    {
        foreach (Days d in Enum.GetValues(typeof(Days)))
        {
            Console.WriteLine(d);
        }
    }
}

输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-enum.html

来源:LearnFk无涯教程网

Sun
Mon
Tue
Wed
Thu
Fri
Sat

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

大模型应用开发实战 -〔黄佳〕

分布式数据库30讲 -〔王磊〕

互联网人的英语私教课 -〔陈亦峰〕

系统性能调优必知必会 -〔陶辉〕

JavaScript核心原理解析 -〔周爱民〕

Flutter核心技术与实战 -〔陈航〕

玩转webpack -〔程柳锋〕

趣谈Linux操作系统 -〔刘超〕

从0开始学架构 -〔李运华〕

好记忆不如烂笔头。留下您的足迹吧 :)