C#初始化数组使用方法和示例

     public static void PrintArray()
        {
            string[] arrayA = { "Shirdrn", "Hamtty", "Saxery" };

            Console.WriteLine("第一种声明数组并初始化的方法");
            for (int i = 0; i < arrayA.Length; i++)
            {
                string arr = arrayA[i];
                Console.WriteLine("arrayA[{0}] = {1}", i, arr);
            }

            string[] arrayB;
            arrayB = new string[3] { "shirdrn", "Hamtty", "Saxery" };
            Console.WriteLine("<b>第二种声明数组并初始化的方法:</b><br>");
            for (int i = 0; i < arrayB.Length; i++)
            {
                string arr = arrayB[i];
                Console.WriteLine("arrayB[{0}] = {1}", i, arr);
            }

            string[] arrayC = new string[3];
            arrayC[0] = "Shirdrn";
            arrayC[1] = "Hamtty";
            arrayC[2] = "Saxery";
            Console.WriteLine("第三种声明数组并初始化的方法");
            for (int i = 0; i < arrayC.Length; i++)
            {
                string arr = arrayC[i];
                Console.WriteLine("arrayC[{0}] = {1}", i, arr);
            }

        }

        public static void PrintArrayB()
        {
            string[,] multiArrayA = { { "Shirdrn", "Hamtty", "Tuuty" }, { "New York", "Beijing", "Shanghai" } };
            Console.WriteLine("第一种声明数组并初始化的方法:");
            for (int i = 0; i < multiArrayA.Rank; i++)
            {
                for (int j = 0; j <= multiArrayA.GetUpperBound(multiArrayA.Rank - 1); j++)
                {
                    string arr = multiArrayA[i, j];
                    Console.WriteLine("multiArrayA[" + i + "][" + j + "] = " + arr + "<br>");
                }
            }

            string[,] multiArrayB = new string[2, 3] { { "Shirdrn", "Hamtty", "Tuuty" }, { "New York", "Beijing", "Shanghai" } };
            Console.WriteLine("第二种声明数组并初始化的方法:");
            for (int i = 0; i < multiArrayB.Rank; i++)
            {
                for (int j = 0; j <= multiArrayB.GetUpperBound(multiArrayB.Rank - 1); j++)
                {
                    string arr = multiArrayA[i, j];
                    Console.WriteLine("multiArrayB[" + i + "][" + j + "] = " + arr + "<br>");
                }
            }

            string[,] multiArrayC = new string[2, 3];
            multiArrayC[0,0] = "Shirdrn";
            multiArrayC[0,1] = "Hamtty";
            multiArrayC[0,2] = "Tuuty";
            multiArrayC[1,0] = "New York";
            multiArrayC[1,1] = "Beijing";
            multiArrayC[1,2] = "Shanghai";
            Console.WriteLine("第二种声明数组并初始化的方法:");
            for (int i = 0; i < multiArrayC.Rank; i++)
            {
                for (int j = 0; j <= multiArrayC.GetUpperBound(multiArrayC.Rank - 1); j++)
                {
                    string arr = multiArrayA[i, j];
                    Console.WriteLine("multiArrayC[" + i + "][" + j + "] = " + arr + "<br>");
                }
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

运行结果:图截了一半

这里写图片描述