1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace RefAndOut
7 {
8 class RefAndOutTesting
9 {
10 //属性(是方法,不是变量)所以不可以作为ref和out参数
11 public static int firstNumber
12 {
13 get;
14 set;
15 }
16 public static int secondNumber
17 {
18 get;
19 set;
20 }
21
22 //字段可以作为ref和out参数
23 public static int myNumber;
24 public static int yourNumber;
25
26 static void Main(string[] args)
27 {
28 //正确
29 OutTest(out myNumber, out yourNumber);
30 Console.WriteLine( "myNumber = {0}, yourNumber = {1}", myNumber, yourNumber);
31 RefTest(ref myNumber, ref yourNumber);
32 Console.WriteLine( "myNumber = {0}, yourNumber = {1}", myNumber, yourNumber);
33
34 //错误
35 OutTest(out firstNumber, out secondNumber);
36 Console.WriteLine("firstNumber = {0}, secondNumber = {1}", firstNumber, secondNumber);
37 RefTest(ref firstNumber, ref secondNumber);
38 Console.WriteLine("firstNumber = {0}, secondNumber = {1}", firstNumber, secondNumber);
45 }
46 }
47 }