C# Array Reverse(Array, Int32, Int32)

Description

Array Reverse(Array, Int32, Int32) reverses the sequence of the elements in a range of elements in the one-dimensional Array.

Syntax

Array.Reverse(Array, Int32, Int32) has the following syntax.


public static void Reverse(
  Array array,/*from   ww w . j  ava2  s  .co m*/
  int index,
  int length
)

Parameters

Array.Reverse(Array, Int32, Int32) has the following parameters.

  • array - The one-dimensional Array to reverse.
  • index - The starting index of the section to reverse.
  • length - The number of elements in the section to reverse.

Returns

Array.Reverse(Array, Int32, Int32) method returns

Example

The following code example shows how to reverse the sort of the values in a range of elements in an Array.


using System;/*from ww  w  . j  a v  a 2 s . c o  m*/
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.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version