区别:
- 值类型和引用类型在内存上存储的地方不一样
- 在传递值类型和传递引用类型的时候,传递的方式不一样,值类型我们称之为值传递,引用类型称之为引用传递
值类型:int,double,bool,char,decimal,struct,enum
引用类型:string,自定义类
存储:
值类型的值是存储在内存的栈当中
引用类型的值是存储在内存的堆当中
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _04值类型和引用类型 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //值类型:int double char decimal bool enum struct 14 //引用类型:string 数组 自定义类 集合 object 接口 15 16 //值传递和引用传递 17 //int n1 = 10; 18 //int n2 = n1; 19 //n2 = 20; 20 //Console.WriteLine(n1); 21 //Console.WriteLine(n2); 22 //Console.ReadKey(); 23 24 //Person p1 = new Person(); 25 //p1.Name = "张三"; 26 //Person p2 = p1; 27 //p2.Name = "李四"; 28 //Console.WriteLine(p1.Name); 29 //Console.ReadKey(); 30 31 //Person p = new Person(); 32 //p.Name = "张三"; 33 //Test(p); 34 //Console.WriteLine(p.Name); 35 //Console.ReadKey(); 36 37 string s1 = "张三"; 38 string s2 = s1; 39 s2 = "李四"; 40 Console.WriteLine(s1); 41 Console.WriteLine(s2); 42 Console.ReadKey(); 43 44 int number = 10; 45 TestTwo(ref number); 46 Console.WriteLine(number); 47 Console.ReadKey(); 48 } 49 //int n=number; 50 public static void TestTwo(ref int n) 51 { 52 n += 10; 53 } 54 55 //Person pp=p; 56 public static void Test(Person pp) 57 { 58 Person p = pp; 59 p.Name = "李四"; 60 } 61 } 62 63 public class Person 64 { 65 private string _name; 66 public string Name 67 { 68 get { return _name; } 69 set { _name = value; } 70 } 71 } 72 }
序列化:就是将对象转换为二进制
返序列化:就是将二进制转换为对象
作用:传输数据
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.IO; 7 using System.Runtime.Serialization.Formatters.Binary; 8 namespace _05序列化和反序列化 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 //要将p这个对象 传输给对方电脑 15 //Person p = new Person(); 16 //p.Name = "张三"; 17 //p.Age = 19; 18 //p.Gender = '男'; 19 //using (FileStream fsWrite = new FileStream(@"C:\Users\SpringRain\Desktop\111.txt", FileMode.OpenOrCreate, FileAccess.Write)) 20 //{ 21 // //开始序列化对象 22 // BinaryFormatter bf = new BinaryFormatter(); 23 // bf.Serialize(fsWrite, p); 24 //} 25 //Console.WriteLine("序列化成功"); 26 //Console.ReadKey(); 27 28 //接收对方发送过来的二进制 反序列化成对象 29 Person p; 30 using (FileStream fsRead = new FileStream(@"C:\Users\SpringRain\Desktop\111.txt", FileMode.OpenOrCreate, FileAccess.Read)) 31 { 32 BinaryFormatter bf = new BinaryFormatter(); 33 p = (Person)bf.Deserialize(fsRead); 34 } 35 Console.WriteLine(p.Name); 36 Console.WriteLine(p.Age); 37 Console.WriteLine(p.Gender); 38 Console.ReadKey(); 39 } 40 } 41 42 43 [Serializable] 44 public class Person 45 { 46 private string _name; 47 48 public string Name 49 { 50 get { return _name; } 51 set { _name = value; } 52 } 53 54 55 private char _gender; 56 57 public char Gender 58 { 59 get { return _gender; } 60 set { _gender = value; } 61 } 62 63 private int _age; 64 65 public int Age 66 { 67 get { return _age; } 68 set { _age = value; } 69 } 70 } 71 }