Revers the sequence of the elements in a range of Array in CSharp
Description
The following code shows how to revers the sequence of the elements in a range of Array.
Example
// www. ja va 2 s . c o m
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a new Array.
Array myArray=Array.CreateInstance( typeof(String), 9 );
myArray.SetValue( "A", 0 );
myArray.SetValue( "B", 1 );
myArray.SetValue( "2", 2 );
myArray.SetValue( "3", 3 );
myArray.SetValue( "Q", 4 );
myArray.SetValue( "V", 5 );
myArray.SetValue( "R", 6 );
myArray.SetValue( "A", 7 );
myArray.SetValue( "D", 8 );
Console.WriteLine( "The Array initially contains the following values:" );
PrintIndexAndValues( myArray );
Array.Reverse( myArray, 1, 3 );
PrintIndexAndValues( myArray );
}
public static void PrintIndexAndValues( Array myArray ) {
for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
}
}
The code above generates the following result.