Initialize in program loop
All elements in the array can be initialized using the nested loop shown here:
int[,] arr7 = new int[5,4];
for(int i=0; i<5; i++)
{
for(int j=0; i<4; j++)
{
arr7[i,j] = 0; // initialize each element to zero
}
}
kind
In the .NET Framework, arrays are implemented as instances of the Array class. This class provides many useful methods, such as Sort and Reverse.
The following example demonstrates how easy it is to use these methods. First, use the Reverse method to reverse the array elements, and then use the Sort method to sort them:
class ArrayMethods
{
static void Main()
{
// Create a string array of size 5:
string[] employeeNames = new string[5];
// Read 5 employee names from user:
("Enter five employee names:");
for(int i=0; i<; i++)
{
employeeNames[i]= ();
}
// Print the array in original order:
("\nArray in Original Order:");
foreach(string employeeName in employeeNames)
{
("{0} ", employeeName);
}
// Reverse the array:
(employeeNames);
// Print the array in reverse order:
("\n\nArray in Reverse Order:");
foreach(string employeeName in employeeNames)
{
("{0} ", employeeName);
}
// Sort the array:
(employeeNames);
// Print the array in sorted order:
("\n\nArray in Sorted Order:");
foreach(string employeeName in employeeNames)
{
("{0} ", employeeName);
}
}
}
Output
Enter five employee names:
Luca
Angie
Brian
Kent
Beatriz
Array in Original Order:
Luca Angie Brian Kent Beatriz
Array in Reverse Order:
Beatriz Kent Brian Angie Luca
Array in Sorted Order:
Angie Beatriz Brian Kent Luca