没有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 }
时间: 2024-11-05 14:38:28