正文

C# 程序员参考--数组教程二2006-09-03 18:06:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/vc2006/18175.html

分享到:

示例    下面是一个完整的 C# 程序,它声明并实例化上面所讨论的数组。    // arrays.cs    using System;    class DeclareArraysSample    {      public static void Main()      {         // Single-dimensional array         int[] numbers = new int[5];          // Multidimensional array         string[,] names = new string[5,4];          // Array-of-arrays (jagged array)         byte[][] scores = new byte[5][];          // Create the jagged array         for (int i = 0; i < scores.Length; i++)         {            scores[i] = new byte[i+3];         }          // Print length of each row         for (int i = 0; i < scores.Length; i++)         {            Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);         }       }     }输出    Length of row 0 is 3    Length of row 1 is 4    Length of row 2 is 5    Length of row 3 is 6    Length of row 4 is 7初始化数组    C# 通过将初始值括在大括号 ({}) 内为在声明时初始化数组提供了简单而直接了当的方法。下面的示例展示初始化不同类型的数组的各种方法。     注意 如果在声明时没有初始化数组,则数组成员将自动初始化为该数组类型的默认初始值。另外,如果将数组声明为某类型的字段,则当实例化该类型时它将被设置为默认值 null。一维数组    int[] numbers = new int[5] {1, 2, 3, 4, 5};    string[] names = new string[3] {"Matt", "Joanne", "Robert"};    可省略数组的大小,如下所示:     int[] numbers = new int[] {1, 2, 3, 4, 5};    string[] names = new string[] {"Matt", "Joanne", "Robert"};    如果提供了初始值设定项,则还可以省略 new 运算符,如下所示:     int[] numbers = {1, 2, 3, 4, 5};    string[] names = {"Matt", "Joanne", "Robert"};  

阅读(1433) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册