c#中数组如何声明?

如题所述

C#声明数组时,方括号([])必须跟在类型后面,而不是标识符后面。在C#中,将方括号放在标识符后是不合法的语法。 例如: int [] table; // not int table[]; 另一细节是,数组的大小不是其类型的一部分,而在 C 语言中它却是数组类型的一部分。这使您可以声明一个数组并向它分配 int 对象的任意数组,而不管数组长度如何。 int [] numbers; // declare numbers as an int array of any size numbers = new int [10]; // numbers is a 10-element array numbers = new int [20]; // now it's a 20-element array C#声明数组细节讨论: C# 支持一维数组、多维数组(矩形数组)和数组的数组(交错的数组)。下面的示例展示如何声明不同类型的数组: C#声明数组之一维数组: int [] numbers; C#声明数组之多维数组: string [,] names; C#声明数组之数组的数组(交错的): byte [][] scores; C#声明数组之(如上所示)并不实际创建它们。在 C# 中,数组是对象(本教程稍后讨论),必须进行实例化。下面的示例展示如何创建数组: C#声明数组之实例化一维数组: int [] numbers = new int [5]; C#声明数组之实例化多维数组: string [,] names = new string [5,4]; C#声明数组之实例化数组的数组(交错的): byte [][] scores = new byte [5][]; for ( int x = 0; x < scores.Length; x++) { scores[x] = new byte [4]; } C#声明数组之实例化三维的矩形数组: int [,,] buttons = new int [4,5,3]; 甚至可以将矩形数组和交错数组混合使用。例如,下面的代码声明了类型为 int 的二维数组的三维数组的一维数组。 int [][,,][,] numbers; C#声明数组之实例化示例 //下面是一个完整的 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); } } } C#声明数组之实例化示例输出 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#声明数组及实例化相关的内容就向你介绍到这里,希望对你了解和学习C#声明数组及相关内容有所帮助。
温馨提示:内容为网友见解,仅供参考
第1个回答  2015-04-20
楼主看看我的 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace How_to_use_Array { class Program { static void Main() { /***************************** 一维数组 *******************************/ //int[] myint = new int[4] { 1, 2, 3, 4 }; /*声明方式一*/ //int[] myint; //myint = new int[4] { 1, 2, 3, 4 }; /*声明方式二*/ //int[] myint = { 1, 2, 3, 4 }; /*声明方式三*/ //for (int i = 0; i < myint.Length; i++) /*遍历方法一*/ //{ // Console.WriteLine(myint[i]); //} //foreach (int i in myint) /*遍历方法二*/ //{ // Console.WriteLine(i); //} /******************************* 多维数组 ******************************/ //int[, ] myint = new int[,] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 11, 12 } }; /*声明方式一*/ //int[,] myint; //myint = new int[,] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 11, 12 } }; /*声明方式二*/ //int[,] myint = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 11, 12 } }; /*声明方式三*/ //Console.WriteLine("befor: {0}\n", myint[1, 2]); //myint[1, 2] = 100; /*可改变数组指定元素的值*/ //Console.WriteLine("after: {0}\n", myint[1, 2]); //for (int i = 0; i < myint.Rank; i++) //{ // for (int j = 0; j < 4; j++) // { // Console.WriteLine(myint[i, j]); // } //} /*遍历方法一*/ //foreach (int i in myint) //{ // Console.WriteLine(i); //} /*遍历方法二*/ /***************************** 交错数组(数组的数组) *******************************/ //int[][] myint = new int[3][]; //myint[0] = new int[5] { 1, 2, 3, 4, 5 }; //myint[1] = new int[3] { 4, 5, 6 }; //myint[2] = new int[2] { 7, 8 }; /*声明方式一*/ int[][] myint = new int[3][] { new int[5] { 1, 2, 3, 4, 5 }, new int[3] { 4, 5, 6 }, new int[2] { 7, 8 } }; /*声明方式二*/ //for (int i = 0; i < myint.Length; i++) //{ // for (int j = 0; j < myint[i].Length; j++) // { // Console.WriteLine(myint[i][j]); // } //} /*遍历方法一*/ foreach (int[] myint2 in myint) { foreach (int myInt in myint2) { Console.WriteLine(myInt); } } /*遍历方法二*/ Console.Read(); } } }本回答被提问者采纳
相似回答
大家正在搜