Надежное программирование
Убедитесь, что перед использованием массива массивов выполнена инициализация его элементов, как показано далее.
myArray5[0] = new int[7]; myArray5[1] = new int[5]; myArray5[2] = new int[2]; |
How to: Initialize an Array
This example shows three different ways to initialize different kinds of arrays: single-dimensional, multidimensional, and jagged.
Example
// Single-dimensional array (numbers). int[] n1 = new int[4] {2, 4, 6, 8}; int[] n2 = new int[] {2, 4, 6, 8}; int[] n3 = {2, 4, 6, 8}; // Single-dimensional array (strings). string[] s1 = new string[3] {"John", "Paul", "Mary"}; string[] s2 = new string[] {"John", "Paul", "Mary"}; string[] s3 = {"John", "Paul", "Mary"}; // Multidimensional array. int[,] n4 = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} }; int[,] n5 = new int[,] { {1, 2}, {3, 4}, {5, 6} }; int[,] n6 = { {1, 2}, {3, 4}, {5, 6} }; // Jagged array. int[][] n7 = new int[2][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} }; int[][] n8 = new int[][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} }; int[][] n9 = { new int[] {2,4,6}, new int[] {1,3,5,7,9} }; |
Compiling the Code
Copy the code and paste it into the Main method of a console application.
Robust Programming
Array members are automatically initialized to the default initial value for the array type if the array is not initialized at the time it is declared. If the array declaration is a field of a type, then when the type is instantiated, the array will be set to its default value of null.
Инициализация массива
В этом примере показано три различных способа инициализации нескольких видов массивов: одномерного, многомерного и массива массивов.
Пример
// Single-dimensional array (numbers). int[] n1 = new int[4] {2, 4, 6, 8}; int[] n2 = new int[] {2, 4, 6, 8}; int[] n3 = {2, 4, 6, 8}; // Single-dimensional array (strings). string[] s1 = new string[3] {"John", "Paul", "Mary"}; string[] s2 = new string[] {"John", "Paul", "Mary"}; string[] s3 = {"John", "Paul", "Mary"}; // Multidimensional array. int[,] n4 = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} }; int[,] n5 = new int[,] { {1, 2}, {3, 4}, {5, 6} }; int[,] n6 = { {1, 2}, {3, 4}, {5, 6} }; // Jagged array. int[][] n7 = new int[2][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} }; int[][] n8 = new int[][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} }; int[][] n9 = { new int[] {2,4,6}, new int[] {1,3,5,7,9} }; |
Компиляция кода
Скопируйте код и вставьте его в метод Main консольного приложения.
Надежное программирование
Элементы массива автоматически инициализируются значениями по умолчанию, если массив не был инициализирован во время объявления. Если массив объявлен как поле типа, то после создания типа массиву по умолчанию будет присвоено значение null.
How to: Pass Object Arrays to Methods
This example shows how an array of objects is passed to the DisplayMyCollection method, which uses the params keyword to accept any number of arguments.
Example
class MyBoxingClass
{
public static void DisplayMyCollection(params object[] anArray)
{
foreach (object obj in anArray)
{
System.Console.Write(obj + "\t");
}
// Suspend the screen.
System.Console.ReadLine();
}
static void Main()
{
DisplayMyCollection(101, "Visual C# Basics", 2002);
}
}
Compiling the Code
You can compile the example directly using the command line, or paste it into a console application.
Дата добавления: 2022-05-27; просмотров: 123;