关于方法的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(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 }

 

posted @ 2015-12-18 14:33  翻滚吧炒鸡蛋  阅读(349)  评论(0编辑  收藏  举报