我试图让该Main()
方法声明三个名为 firstInt、middleInt 和 lastInt 的整数。并将这些值分配给变量,显示它们,然后将它们传递给接受它们作为引用变量的方法,将第一个值放在 lastInt 变量中,并将最后一个值放在 firstInt 变量中。在该Main()
方法中,再次显示三个变量,证明它们的位置已经颠倒了。
static void Main(string[] args)
{
int first = 33;
int middle = 44;
int last = 55;
Console.WriteLine("Before the swap the first number is {0}", first);
Console.WriteLine("Before the swap the middle number is {0}", middle);
Console.WriteLine("Before the swap the last number is {0}", last);
Swap(ref first, ref middle, ref last);
Console.WriteLine("\n============AFTER===THE===SWAP======================");
Console.WriteLine("After the swap the first is {0}", first);
Console.WriteLine("After the swap the middle is {0}", middle);
Console.WriteLine("After the swap the last is {0}", last);
}
private static void Swap(ref int first, ref int middle, ref int last);
int temp;
temp = firstInt;
firstInt = lastInt;
lastInt = temp;
}
}