2 引用类型
包括 类class、接口interface、代表delegate、数组array
2.1 类
对于值类型,每个变量直接包含自身的所有数据,每创建一个变量,就在内存中开辟一个区域;而对于引用类型,每个变量只存储对目标数据的引用,每创建一个变量,就增加一个指向目标数据的指针。
public static void Main() { //结构 SContact s1 = new SContact(); s1.m_name = "李明"; s1.m_age = 20; s1.m_telephone = "Unknown"; SContact s2 = s1; s2.m_name = "张三"; s2.m_age = 25; s2.m_telephone = "Unknown"; Console.WriteLine("修改结构变量后:"); Console.WriteLine(s1.m_name); Console.WriteLine(s1.m_age); //类 CContact c1 = new CContact(); c1.m_name = "李明"; c1.m_age = 20; CContact c2 = c1; c2.m_name = "张三"; c2.m_age = 25; Console.WriteLine("修改类变量后:"); Console.WriteLine(c1.m_name); Console.WriteLine(c1.m_age); } struct SContact { public string m_name; public int m_age; public string m_telephone; } class CContact { public string m_name; public int m_age; public string m_telephone = "Unknown"; }
输出结果:
修改结构变量后:
李明
20
修改类变量后:
张三
25
请按任意键继续. . .
时间: 2024-10-08 20:55:02