Академический Документы
Профессиональный Документы
Культура Документы
members are automatically initialized to the default initial value for the array
type. Also, if you declare the array as a field of a type, it will be set to the
default value null when you instantiate the type.
Something about foreach
In a for loop, you should know the number of element of the array. If you don't,
the C# language allows you to let the compiler use its internal mechanism to get
this count and use it to know where to stop counting. To assist you with this, C#
provides the foreach operator.
You can also omit the new operator if you initialize array like this:
int[,] numbers = {{1,2}, {3,4}, {5,6}};
string[,] names = {{"Ana", "Anita"}, {"Bob", "Barbie"}};
Practical demonstration of multidimensional array
using System;
namespace array_multidim
{
class Program
{
static void Main(string[] args)
{
//declaring a array with 4 rows and 3 columns
int[,] arr = new int[4, 3];
//accepting values in the array
for (int x = 0; x < 4; x++)
{
Console.WriteLine("Enter row element : {0}", x + 1);
for (int y = 0; y < 3; y++)
{
arr[x, y] = Int32.Parse(Console.ReadLine());
}
}
// displaying values of multidimensional array
for (int x = 0; x < 4; x++)
{
Console.WriteLine("Values of row : {0}", x + 1);
for (int y = 0; y < 3; y++)
{
Console.Write(arr[x, y] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Jagged Arrays (array-of-arrays)
Jagged arrays are often called array of arrays. More specifically, a jagged
array is an array of vectors. Unlike rectangular arrays, a jagged array, as the
name implies, is jagged as each vector in the array can be of different
length.
It is also possible to have jagged arrays, whereby "rows" may be different
sizes. For this, you need an array in which each element is another array.
However, all this is only possible if the arrays have the same base type.
Declaring and initializing jagged array.
int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[3];
jaggedArray[1] = new int[5];
jaggedArray[0] = new int[] { 3, 5, 7, };
jaggedArray[1] = new int[] { 1, 0, 2, 4, 6 };
You can also initialize the array upon declaration like this:
new int[] { 1, 0, 2, 4, 6 }
};
Note: Notice that you cannot omit the new operator from the elements
initialization because there is no default initialization for the elements.
A jagged array is an array of arrays, and therefore its elements are
reference types and are initialized to null.
It is possible to mix jagged and multidimensional arrays. The following is a
declaration and initialization of a single-dimensional jagged array that
contains two-dimensional array elements of different sizes:
int[][,] jaggedArray = new int[4][,]
{
new int[,] { {11,23}, {58,78} },
new int[,] { {50,62}, {45,65}, {85,15} },
new int[,] { {245,365}, {385,135} },
new int[,] { {1,2}, {4,4}, {4,5} }
};
Practical demonstration of jagged array
using System;
namespace array_jagged
{
class Program
{
static void Main(string[] args)
{
// Declare the array of four elements:
int[][] arr = new int[4][];
// Initialize the elements:
arr[0] = new int[2] { 7, 9 };
arr[1] = new int[4] { 12, 42, 26, 38 };
arr[2] = new int[6] { 3, 5, 7, 9, 11, 13 };
arr[3] = new int[3] { 4, 6, 8 };
// Display the array elements:
Arrays are extremely useful when you have several similar objects that you
want to be able to process the same way or to be able to process as a group
rather than individually.
The Array class, defined in the System namespace, is the base class for
arrays in C#. Array class is an abstract base class, which can't be inherited,
but it provides CreateInstance method to construct an array.
System.Array class provides methods for creating, manipulating,
searching, and sorting arrays, thereby serving as the base class for all arrays
in the common language runtime.
Array class properties
IsFixedSize
Length
Rank
SyncRoot
Clear
Clone
Copy
CopyTo
This method copies all the elements of the current onedimensional Array to the specified onedimensional Arraystarting at the specified
destination Array index.
CreateInstance
GetEnumerator
GetLength
GetLowerBound
GetUpperBound
GetValue
IndexOf
Initialize
This method initializes every item of the valuetype Array by calling the default constructor of the value
type.
LastIndexOf
Reverse
This method reverses the order of the items in a onedimensional Array or in a portion of the Array.
SetValue