0

Possible Duplicate:
Logic behind the Array.Reverse method

I can not find an source of this method. Who knows how it is implemented?

char[] inputstream = source.ToCharArray();
Array.Reverse(inputstream);
Community
  • 1
  • 1
Mediator
  • 14,951
  • 35
  • 113
  • 191

1 Answers1

1

Using .NET Reflector :)

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--;
            }
        }
    }
}
hwcverwe
  • 5,287
  • 7
  • 35
  • 63
  • 1
    What is `TrySZReverse`? That is the big question.. – nawfal May 30 '13 at 13:57
  • @nawfal as you can see, if TrySZReserve fails (returns false) it uses a fallback algorithm which is readable and understandable for you and me;) But you are right.. the big question is: Which more efficient algorithm is insize TrySZReserve. I can't decompile that method unfortunately. – hwcverwe Jun 03 '13 at 15:03