C#引用类型ref

C#引用类型ref

C#中的引用类型必须加上ref关键字,数组则不用int []b->int []a(区别于C/C++)

ref参数必须在调用之前明确赋值!

示例代码:
[cpp]

//c#中的引用类型
using System;
class Myclass
{
public void Sort(ref int x, ref int y, ref int z)
{
int tmp;
//把x,y,z从小到大排序
if (x > y) { tmp = x; x = y; y = tmp; };
if (x > z) { tmp = x; x = z; z = tmp; };
if (y > z) { tmp = y; y = z; z = tmp; };
}
}
class Test
{
static void Main()
{
Myclass m = new Myclass();
int a, b, c;
a = 30; b = 20; c = 10;
m.Sort(ref a,ref b,ref c);
Console.WriteLine(“{0} ,{1} ,{2}”,a,b,c);
Console.Read();
}
}

[/cpp]