复杂的变量类型——枚举、结构、数组

来源:互联网 发布:电子软件开发 编辑:程序博客网 时间:2024/06/07 03:31

复杂的变量类型——枚举、结构、数组

下面分别给出三种变量类型的代码示例(亲自验证),每个类型需要注意的事项都有相应的标注。

枚举实例

<span style="font-family:Courier New;">namespace Ch05Ex02{    enum orientation : byte //枚举的基本类型可以是byte,sbyte,short,ushort,int,uint,long,ulong.    {       north=1,      south=2,      east,// 枚举使用一个基本类型来存储。默认是Int,从0开始。没有被赋值的都会被自动获取一个初始值(这个值是之前最后一个所明确声明的值,再加1。如此处east=3)      west,      StringToOritentation,//将string转化为orientation时新加上去的    }    class Program    {        static void Main(string[] args)        {            byte directionByte;            string directionString;            string myString = "StringToOritentation";            orientation myDirection = orientation.east;            Console.WriteLine("the fisrt myDirection={0}",myDirection);            directionByte = (byte)myDirection;//即使orientation的基本类型是byte,仍然需要使用(byte)进行数据类型转换。            directionString = Convert.ToString(myDirection);//将oritentation转化为string,代码有两种写法 directionString = myDirection.ToString();            Console.WriteLine("directionByte={0}",directionByte);            Console.WriteLine("directionString={0}", directionString);            myDirection = (orientation)Enum.Parse(typeof(orientation),myString);//将string转化为orientation.前提是枚举值中存在myString,否则会报错.            Console.WriteLine("the second myDirection={0}", myDirection);            Console.ReadKey();                   }    }}</span>


结构实例

<span style="font-family:Courier New;">namespace Ch05Ex03{    enum orientation : byte    {        north=1,        south,        east,        west,        }    struct route    {        public orientation direction;        public double distance;        }    class Program    {        static void Main(string[] args)        {            int myDirection;            route myRoute;            Console.WriteLine("1)north\n 2)south\n 3)east\n 4)west\n");                       do            {                Console.WriteLine("Please select a direction:");//将该语句写在循环内部,如果用户输入错误就会再次提示。                myDirection = Convert.ToInt32(Console.ReadLine());//注意不要写成ReadKey().                          } while (myDirection < 1 || myDirection > 4);                       myRoute.direction = (orientation)myDirection;//注意转换            Console.WriteLine("Please input the distance:");            myRoute.distance=Convert.ToDouble(Console.ReadLine());            Console.WriteLine("myDirection is {0},myDistance is {1}.",myRoute.direction,myRoute.distance);//占位符用{}            Console.ReadKey();        }    }}</span>


数组实例

<span style="font-family:Courier New;"> static void Main(string[] args)        {            int[] number = new int[5] { 1, 2, 3, 4, 5 };//一维数组            foreach (int Number in number) //大小写区别对待            {                Console.WriteLine("{0}", Number);            }            double[,] hillHeight = { { 1, 3, 5 }, { 2, 4, 6 } };//两行三列            foreach (double height in hillHeight)            {                Console.WriteLine("{0}", height);//先行后列            }            double[][] divorsors = {                             //多行多列,不是标准的矩阵型数组。                                       new double[]{1,2,3},                                       new double[]{1},                                       new double[]{1,2,3,4,5,6,},                                       new double[]{0}                                                                      };            foreach(double[] divorsorsOut in divorsors)  //嵌套输出            {                foreach(double divorsorsIn in divorsorsOut )              {                  Console.Write("{0}",divorsorsIn);                          }            }                        Console.ReadKey();        }</span>












0 0
原创粉丝点击