C#编程题型(急)

来源:百度知道 编辑:UC知道 时间:2024/06/17 01:48:43
将交换数组的两个元素的功能用一个Swap()方法实现,Main()方法负责输出,Swap()方法的参数按引用方式传递

你想要的是这个么?

class Program
{
static void Swap(ref int[] array, int index1, int index2)
{
try
{
int tmp = array[index1];
array[index1] = array[index2];
array[index2] = tmp;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

static void Main()
{
int index1 = 4, index2 = 6;
int[] integerArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Console.WriteLine("Before swap:");
foreach(int i in integerArray)
{
Console.Write(i+" ");
}
Console.WriteLine("\n\nSwap item {0} and {1} (item index is from 0 to 7).", index1, index2);