1

what is the native logic work behind public static void Reverse(Array array, int index, int length);

abatishchev
  • 98,240
  • 88
  • 296
  • 433
prantick
  • 21
  • 2
  • 1
    With two correct answers to this topic, all thats left to me is suggest ".NET Reflector" to you ... with this tool you can disassemble this method by yourself. (Or you could download the .NET sources) – Hinek May 19 '10 at 14:35

3 Answers3

5

You can use .NET Reflector for that:

[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
public static void Reverse(Array array, int index, int length)
{
    if (array == null)
    {
        throw new ArgumentNullException("array");
    }
    if ((index < array.GetLowerBound(0)) || (length < 0))
    {
        throw new ArgumentOutOfRangeException((index < 0) ? "index" : "length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
    }
    if ((array.Length - (index - array.GetLowerBound(0))) < length)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
    }
    if (array.Rank != 1)
    {
        throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
    }
    if (!TrySZReverse(array, index, length))
    {
        int num = index;
        int num2 = (index + length) - 1;
        object[] objArray = array as object[];
        if (objArray == null)
        {
            while (num < num2)
            {
                object obj3 = array.GetValue(num);
                array.SetValue(array.GetValue(num2), num);
                array.SetValue(obj3, num2);
                num++;
                num2--;
            }
        }
        else
        {
            while (num < num2)
            {
                object obj2 = objArray[num];
                objArray[num] = objArray[num2];
                objArray[num2] = obj2;
                num++;
                num2--;
            }
        }
    }
}

TrySZReverse is a native method that can sometimes do the same thing only faster.

Sky Sanders
  • 36,396
  • 8
  • 69
  • 90
2

Loop from the starting point, index, to the middle of the range, index + length/2, swapping each array[i] with array[index + length - i - 1].

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
1

Some details about the native method "TrySZReverse"

from the related codes from coreclr([1][2]), TrySZReverse handles the array of primitive types, and the reverse algorithm is the same as Array.Reverse :

codes quotation

    static void Reverse(KIND array[], UINT32 index, UINT32 count) {
        LIMITED_METHOD_CONTRACT;

        _ASSERTE(array != NULL);
        if (count == 0) {
            return;
        }
        UINT32 i = index;
        UINT32 j = index + count - 1;
        while(i < j) {
            KIND temp = array[i];
            array[i] = array[j];
            array[j] = temp;
            i++;
            j--;
        }
    }

and the prefix "SZ" seems stands for "single-dimension zero-terminated".

tkokof
  • 101
  • 1
  • 9