using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Swap(ref int x,ref int y)
{
int temp = x;
x = y;
y = temp;
}
static void Main(string[] args)
{
int a = 40;
int b = 80;
Console.WriteLine("交换前: a = {0},b = {1}",a,b);
Swap(ref a,ref b);
Console.WriteLine("交换后: a = {0},b = {1}",a,b);
Console.ReadKey();
}
}
}
交换前: a = 40,b = 80
交换后: a = 80,b = 40
ref修饰的变量必须要赋值
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Swap(ref int x,ref int y)
{
int temp = x;
x = y;
y = temp;
}
static void Main(string[] args)
{
int a = 40;
int b = 80;
Console.WriteLine("交换前: a = {0},b = {1}",a,b);
Swap(ref a,ref b);
Console.WriteLine("交换后: a = {0},b = {1}",a,b);
Console.ReadKey();
}
}
}
交换前: a = 40,b = 80
交换后: a = 80,b = 40
ref修饰的变量必须要赋值