结构和类的引用
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace class_struct
6 {
7 public struct Student
8 {
9 public string name;
10 }
11 public class Teacher
12 {
13 public string name;
14 }
15
16
17 public class Program
18 {
19 public void Swap(int x, int y)
20 {
21 int temp = x;
22 x = y;
23 y = temp;
24
25 }
26 public void ChangeStruct(Student st)
27 {
28 st.name ="LIN";
29 }
30 public void ChangeClass(Teacher t)
31 {
32 t.name ="wjinbd";
33 }
34
35 public void Swap(string x, string y)
36 {
37 string temp = x;
38 x=y;
39 y=temp;
40 }
41 static void Main(string[] args)
42
43 {
44 int a = 10, b = 15;
45 Console.WriteLine("before a={0},b={1}", a, b);//未使用Swap方法之前 结果未10,15
46 Program p = new Program();
47 p.Swap(a ,b);
48 Console.WriteLine("after a={0},b={1}",a,b);//使用Swap方法交换之后,结果是10,15,说明值类型是拷贝一个副本,出了方法值还是一样的 使用ref后 结果才能交换
49 Student st = new Student();
50 st.name = "zxlin25";//初始值
51 p.ChangeStruct(st);//使用方法后
52 Teacher t = new Teacher();
53 t.name = "wj";//初始值
54 p.ChangeClass(t);//使用方法后
55
56 Console.WriteLine(t.name);//结果为方法后的值:wjinbd
57 Console.WriteLine(st.name);//结果未初始值 zxlin25
58 // p.Swap(p.x,p.y);
59 // Console.WriteLine(p.x+","+p.y);
60 //Console.WriteLine(p.x);
61 Console.ReadLine();
62 }
63 }
64 }
65
66