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();
}
}
}
枚举是种数据类型,包含多个固定的值.在利用一组常量时可以使用枚举类型.
结构好比一个模板.在此模板中声明变量.以后使用此模板中的变量时无须再声明.
看下面代码:
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();
}
}
}
相关文章推荐
- 【enum】如何在枚举中定义自定义的方法,并进行使用
- C#枚举enum定义使用
- 【enum】如何在枚举中定义自定义的方法,并进行使用
- struct 结构及enum枚举
- 已知有枚举类型定义:enum MyEn{a=101,b,c=207,d,e,f,g},编程输出第5个枚举元素的序号值
- Struct2_定义拦截器并使用注解方式作用在Action的方法中
- C#编程->FreeImage.dll使用方法
- 使用结构(C# 编程指南)
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- 使用 Split 方法分析字符串(C# 编程指南)
- 【C#基础】枚举(Enum)、结构体(Struct)、委托(Delegate)
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- 类里定义的枚举使用方法
- C#调用C++方法,C#使用c++方法返回类或结构体
- 结构struct 联合Union和枚举Enum的细节讨论
- Java enum(枚举)使用方法详解
- 14. JAVA 枚举(Enum、类集EnumMap&EnumSet 、实现接口、定义抽象方法) ----- 学习笔记
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- C#编程:类定义中可以使用的访问修饰符
- c#经典编程实例(二方法的使用)