ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

值类型和引用类型 序列化和反序列化

2022-01-17 16:32:53  阅读:117  来源: 互联网

标签:Console string System Person 引用 WriteLine 类型 using 序列化


区别:

  1. 值类型和引用类型在内存上存储的地方不一样
  2. 在传递值类型和传递引用类型的时候,传递的方式不一样,值类型我们称之为值传递,引用类型称之为引用传递

值类型: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 }

 

标签:Console,string,System,Person,引用,WriteLine,类型,using,序列化
来源: https://www.cnblogs.com/xhj8/p/15814009.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有