• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一蓑烟雨
C/C++,Linux,语音技术
博客园    首页    新随笔    联系   管理    订阅  订阅
枚举与结构体

1、结构体和枚举一样,都是在代码的主体外部声明的。

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace StructEnum
7 {
8 enum orientation : byte
9 {
10 north = 1,
11 south = 2,
12 east = 3,
13 west = 4
14 }
15 struct route
16 {
17 public orientation direction;
18 public double distance;
19
20 }
21 class Program
22 {
23 static void Main(string[] args)
24 {
25 route myRoute;
26 double myDistance;
27 int myDirection;
28 myDistance = Convert.ToDouble(Console.ReadLine());
29 myDirection = Convert.ToInt32(Console.ReadLine());
30 myRoute.distance = myDistance;
31 myRoute.direction = (orientation)myDirection;
32 Console.WriteLine("distance:{0},direction:{1}",myRoute.distance,myRoute.direction);
33 }
34 }
35 }

输入4 4

输出 4 west

2.C#结构体与C结构体类似
3.虽然枚举有类型,但是取枚举的值时,仍然需要进行类型转换

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace StructEnum
7 {
8 enum orientation:byte
9 {
10 north = 1,
11 south = 2,
12 east = 3,
13 west = 4
14 }
15 struct route
16 {
17 public orientation direction;
18 public double distance;
19 public int dirct;
20 }
21 class Program
22 {
23 static void Main(string[] args)
24 {
25 route myRoute,mRoute;
26 double myDistance;
27 int myDirection;
28 myDistance = Convert.ToDouble(Console.ReadLine());
29 myDirection = Convert.ToInt32(Console.ReadLine());
30 myRoute.distance = myDistance;
31 myRoute.direction = (orientation)myDirection;
32 mRoute.dirct = orientation.north; //output north
33 Console.WriteLine("distance:{0},direction:{1}",myRoute.distance,myRoute.direction);
34 Console.WriteLine("mRoute.dirct={0}",mRoute.dirct);
35 }
36 }
37 }

第32行,编辑错误。

把 mRoute.dirct = orientation.north; 改为 mRoute.dirct = (int)orientation.north; 

输出mRoute.dirct = 1。
 

posted on 2011-11-28 10:26  lovemu  阅读(2878)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3