首页 > 代码库 > C#练习笔记4:枚举和数组练习

C#练习笔记4:枚举和数组练习

  枚举的意义,位标志,Flags,实现和比较。数组的分类,主要练习矩形数组和交错数组

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {

            //枚举
            Console.WriteLine("枚举:");
            MyEnum me1 = MyEnum.red;
            MyEnum me2 = MyEnum.green;


            //位运算
            MyEnum me = me1 | me2;
       //可以进行与,或运算等
            MyEnum mea = MyEnum.red | MyEnum.green;
            Console.WriteLine(mea.GetType());

            Console.WriteLine( mea.HasFlag(me));//是否包含me里面的所有枚举
            Console.WriteLine(mea==me);
            Console.WriteLine((int)mea);
            Console.WriteLine(me);
            Console.WriteLine((int)me);
            Console.WriteLine();
            //数组,
            Console.WriteLine("数组:");
            int[][,] ay = new int[2][,];
            ay[0] = new int[,]
            {
                {5,60},
                {6,32 }
            };
            ay[1] = new int[,]
            {
                {65,5,6},
                {4,3 ,5}
            };

            Console.WriteLine("第一个数组:");
            for (int i = 0; i < ay.Length; i++)
            {
                for (int j = 0; j < ay[i].GetLength(0); j++)//这里获取的是ay[i]的第一维度的长度
                {
                    for (int k = 0; k <ay[i].GetLength(1);k++)//这里获取的是第二维度的长度
                    {
                        Console.Write("ay[{0}][{1},{2}]={3}\t",i,j,k,ay[i][j,k]);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }


            Console.WriteLine("第二个数组:");
            int[][] array=new int[3][];
            array[0] = new int[] { 5,3};
            array[1] = new int[] { 5, 6, 9 };
            array[2] = new int[] { 6, 5, 9, 6 };
            Console.WriteLine(array.Length);
            for (int i = 0; i < array.Length; i++)
            {
                for(int j=0;j<array[i].GetLength(0);j++)
                {
                    Console.WriteLine("array[{0}][{1}]={2}\t",i,j,array[i][j]);
                }
                Console.WriteLine();
            }


            Console.WriteLine("第三个数组:");
            int[][] arr = new int[2][]
            {
                new int[]{6,5 },
                new int[]{ 6,9,3}
            };
            for(int i=0;i<arr.Length;i++)
            {
                for(int j=0;j<arr[i].GetLength(0);j++)
                {
                    Console.WriteLine("arr[{0}][{1}]={2}\t",i,j,arr[i][j]);
                }
                Console.WriteLine();
            }


            //矩形数组
            int[,] arry = new int[2, 3]
            {
                {1,2,3 },
                { 1,6,9},
            };

            Console.Read();
        }
    }


    //枚举
    [Flags]//位标志
     enum MyEnum:int
    {
        red=0x02,
        green=0x04,
        black=0x08
    }
}

  运行结果:

  技术分享

  技术分享

C#练习笔记4:枚举和数组练习