首页 > 其他 > 详细

关于方法的ref

时间:2015-12-18 16:07:13      阅读:120      评论:0      收藏:0      [点我收藏+]

没有ref的方法时:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication7
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int number = 10;
14             Test(number);
15             Console.WriteLine(number);//输出结果还是10,方法并没有改变number的值
16             Console.ReadKey();
17         }
18 
19         static int Test(int a)
20         {
21             a = 100;
22         }
23         
24     }
25 }

有ref的方法时://ref 双向传递值

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication7
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int number = 10;
14             Test(ref number);
15             Console.WriteLine(number);//输出结果是100,方法改变了number的值
16             Console.ReadKey();
17         }
18 
19         static int Test(ref int a)
20         {
21             a = 100;
22         }
23         
24     }
25 }

 

关于方法的ref

原文:http://www.cnblogs.com/start-from-scratch/p/5057005.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!