您的位置:首页 > 编程语言 > C#

C# 中传参中的OUT 和 ref 区别 笔记

2016-04-10 19:52 344 查看
//out传参前需要对参数进行赋值处理,ref则不需要。
//out、ref 传参都可以对值进行改变
1 static void Main(string[] args)
{
int I = 10;
//int J = 10;
//int I;
int J;
//int[] k=new int[5]{1,2,3,4,5};
int[] k = new int[5];
//int[] l = new int[5]{1,2,3,4,5};
int[] l = new int[5];

TestClass1 tc = new TestClass1();
tc.testClass(ref I);
tc.testClass1(out J);

tc.testClass2(out k);
tc.testClass3(ref l);

Console.WriteLine("out I:"+I);
Console.WriteLine("ref J:" + J);

Console.WriteLine("out k[0]:" + k[0]);
Console.WriteLine("ref l[0]:" + l[0]);
Console.Read();
}
public  void testClass(ref int i)
{
i = 100;
}
public void testClass1(out int i)
{
i = 100;
}
public void testClass2(out int[] k)
{
k=new int[5];
k[0] = 100;
}
public void testClass3(ref int[] l)
{
l = new int[5];
l[0] = 100;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: