c#数组

    在 C# 中,数组实际上是对象,而不只是像 C 和 C++ 中那样的可寻址连续内存区域。Array 是所有数组类型的抽象基类型。可以使用 Array 具有的属性以及其他类成员。这种用法的一个示例是使用 Length 属性来获取数组的长度。下面的代码将 numbers 数组的长度(为 5)赋给名为 lengthOfNumbers 的变量:

    具体详见microsoft 的MSDN:  http://msdn.microsoft.com/zh-cn/library/9b9dty7d(VS.80).aspx

    下面给出一个完整的示例,动态指定数组的长度:

  class SentArray
    {
        public void PrintArray(int ArrLength) //这个方法的作用是给数组中的项赋值并在屏幕上打印出来
        {

            int[] arr = new int[ArrLength];
            for (int i = 0; i < arr.Length; i++)
                arr[i] = i;
            Console.WriteLine("The Array Values are:");
            for (int j = 0; j < arr.Length; j++)
                Console.WriteLine("arr[{0}]={1}", j, arr[j]);

        }
   
    }
   
   
    class Program
    {
      
        static void Main(string[] args)
        {
            //int[] arr = new int[] { 1, 2, 3 };
            //int[] arr ={ 1, 2, 3,4,5,6,7,8 };
            //int[] arr = new int[10];
            //arr[0]=1;
            //arr[1]=2;
            //arr[2]=3;
            //for(int i=0;i<arr.Length;i++)
            //{
            //   Console.WriteLine("{0}",arr[i]);
              
            //}
            //foreach (int i in arr)
            //    Console.WriteLine(i);
            //Console.ReadLine();
            SentArray arr = new SentArray();//实例化SetArray类
            int lengh = 1;
            while (lengh> 0)
            {
                Console.WriteLine("Please Enter The Array's Length:");
                lengh = Int32.Parse(Console.ReadLine());//格式转换
                arr.PrintArray(lengh);//调用对象的PrintArray
           
            }


        }
    }

原文地址:https://www.cnblogs.com/liuzhengliang/p/1343301.html