using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Class1
{
static void Main(string[] args)
{
// 传值
int i1 = 1, i2 = 2;
swap(i1, i2);
Console.WriteLine("{0} {1}", i1, i2);
// 传址,ref传址之前一定要先赋值
int i3 = 1, i4 = 2;
swap(ref i3, ref i4);
Console.WriteLine("{0} {1}", i3, i4);
// 传址,out传址之前不用先赋值,即使赋值了也没用,传址以后一定要赋值
int i5 = 1, i6 = 2;
swap2(out i5, out i6);
Console.WriteLine("{0} {1}", i5, i6);
Console.ReadKey();
}
/**
* 交换数据i1和i2
*/
static void swap(int i1, int i2)
{
int temp;
temp = i1;
i1 = i2;
i2 = temp;
}
/**
* 交换数据i1和i2
*/
static void swap(ref int i1, ref int i2)
{
int temp;
temp = i1;
i1 = i2;
i2 = temp;
}
/**
* 交换数据i1和i2
*/
static void swap2(out int i1, out int i2)
{
// 传址以后一定要赋值
i1 = 5;
i2 = 6;
int temp;
temp = i1;
i1 = i2;
i2 = temp;
}
}
}
传址,ref,out
最新推荐文章于 2023-10-29 07:59:11 发布