您的位置:首页 > 编程语言 > C#

C#编程:枚举(enum)与结构(struct)的定义和使用方法

2009-03-30 10:29 621 查看
C#编程:枚举(enum)与结构(struct)的定义和使用方法

枚举是种数据类型,包含多个固定的值.在利用一组常量时可以使用枚举类型.
结构好比一个模板.在此模板中声明变量.以后使用此模板中的变量时无须再声明.
看下面代码:

namespace ConsoleApplication1

{

//定义枚举.枚举所使用的类型只能为:sbyte, byte, short, ushort, int, uint, long, ulong

enum student : sbyte {

name = 1,

age = 12,

sex = -11

}

//定义结构

public struct studentInfo {

public string name;

public int age;

public string sex;

}

class Program

{

static void Main(string[] args)

{

//枚举的使用方法开始

int info = Convert.ToInt16(student.name);

Console.WriteLine("我的名字叫:{0}", info);

Console.ReadKey();

//结构的使用方法开始

studentInfo cngothicInfo = new studentInfo();

cngothicInfo.name = "cndeath";

cngothicInfo.age = 23;

cngothicInfo.sex = "男";

Console.WriteLine("结构name:{0}", cngothicInfo.name);

Console.ReadKey();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: