码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-04-10 21:14:45      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

//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 }

 

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

标签:

原文地址:http://www.cnblogs.com/sdjgl/p/5360526.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!