枚举类型在C#或java,VB等一些计算机编程语言中是一种基本数据类型,在C++,C中是一种构造数据类型。枚举类型的实质是一些有名字的整型常量的集合。枚举可以根据Integer、Long、Short或Byte中的任意一种数据类型来创建一种新型变量。它用于声明一组命名的常数,当一个变量有几种可能的取值时,可以将它定义为枚举类型。定义枚举类型后,系统不会为之分配内存,当定义该类型的变量后,才会为变量分配内存,大小为枚举中所有成员内存之和。
c#枚举举例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace enum_struct
{
enum orientation
{
north,//默认从0开始
south,
east,
wes
}
enum day
{
monday=1,
tuesday,
wednesday,
thursday,
friday,
satuarday,
sunday
}
class Program
{
static void Main(string[] args)
{
orientation or=orientation.south;
Console.WriteLine(or.ToString());
Console.WriteLine(Convert.ToInt32(or));
Console.WriteLine("---------------------");
Console.WriteLine(day.tuesday.ToString());
Console.WriteLine(Convert.ToInt32(day.tuesday));
Console.ReadKey();
}
}
}