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

//out传参前需要对参数进行赋值处理,ref则不需要。
//out、ref 传参都可以对值进行改变
1
static void Main(string[] args) 2 { 3 int I = 10; 4 //int J = 10; 5 //int I; 6 int J; 7 //int[] k=new int[5]{1,2,3,4,5}; 8 int[] k = new int[5]; 9 //int[] l = new int[5]{1,2,3,4,5}; 10 int[] l = new int[5]; 11 12 TestClass1 tc = new TestClass1(); 13 tc.testClass(ref I); 14 tc.testClass1(out J); 15 16 tc.testClass2(out k); 17 tc.testClass3(ref l); 18 19 Console.WriteLine("out I:"+I); 20 Console.WriteLine("ref J:" + J); 21 22 Console.WriteLine("out k[0]:" + k[0]); 23 Console.WriteLine("ref l[0]:" + l[0]); 24 Console.Read(); 25 } 26 public void testClass(ref int i) 27 { 28 i = 100; 29 } 30 public void testClass1(out int i) 31 { 32 i = 100; 33 } 34 public void testClass2(out int[] k) 35 { 36 k=new int[5]; 37 k[0] = 100; 38 } 39 public void testClass3(ref int[] l) 40 { 41 l = new int[5]; 42 l[0] = 100; 43 } 44 45 46 }

 

posted @ 2016-04-10 19:52  圣诞节到了  阅读(174)  评论(0编辑  收藏  举报