| 范文 | C#的Array对象自带反转功能,但是下面的代码完全通过自定义的算法来实现数组反转
 代码如下:
 public static void ReverseArray<T>(this T[] inputArray)
 {
 T temp = default(T);
 if (inputArray == null)
 throw new ArgumentNullException("inputArray is empty");
 if (inputArray.Length > 0)
 {
 for (int counter = 0; counter < (inputArray.Length / 2); counter++)
 {
 temp = inputArray[counter];
 inputArray[counter] = inputArray[inputArray.Length - counter - 1];
 inputArray[inputArray.Length - counter - 1] = temp;
 }
 }
 else
 {
 Trace.WriteLine("Reversal not needed");
 }
 }
 |