惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CERT Recently Published Vulnerability Notes
U
Unit 42
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
L
Lohrmann on Cybersecurity
Blog — PlanetScale
Blog — PlanetScale
Recorded Future
Recorded Future
D
DataBreaches.Net
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
I
Intezer
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
I
InfoQ
宝玉的分享
宝玉的分享
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
博客园 - 司徒正美
H
Hacker News: Front Page
Y
Y Combinator Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
NISL@THU
NISL@THU
月光博客
月光博客
有赞技术团队
有赞技术团队
Cloudbric
Cloudbric
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
A
Arctic Wolf
博客园 - 【当耐特】
W
WeLiveSecurity
V
Visual Studio Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
V2EX
C
Cyber Attacks, Cyber Crime and Cyber Security
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog

博客园 - KiddLee

数据库存储图像及使用Image控件显示 三层体系结构总结(六) Spring.Core IoC(一) Spring.Net 模块组成 Generating user instances in Sql Server is disabled. Use sp_configure 'user instances enabled' to generate user instances 面向对象分析设计学习与探索(六):好的设计=软件的灵活程度(good design=flexible software)继续 面向对象分析设计学习与探索(六):面向对象的灾难(OO Catastrophe) 面向对象分析设计学习与探索(六):好的设计=软件的灵活程度(good design=flexible software) 面向对象分析设计学习与探索(五):分析(Analysis) 面向对象分析设计学习与探索(四):需求变化(Requirements Change) 面向对象分析设计学习与探索(三):收集需求(Gathering Requirement) 面向对象分析设计学习与探索(二):好的应用程序设计(Well-designed apps rock) 面向对象分析设计学习与探索(一):开篇 类型构造器 装箱拆箱续 Session Cookie and Persistent Cookie 第一次执行方法 我犯错了 接口函数还可以声明为private
装箱拆箱
KiddLee · 2007-03-22 · via 博客园 - KiddLee

我原来对于装拆箱的理解只是停留在将值类型转化为Object类型,然后再转换回来。实际上,在这个看似简单的问题上还有一层更深的含义。

值类型和引用类型在实例化的时候是存储在不同的空间中的,值类型存储在Stack上,而引用类型是存储在Heap上,在装箱的过程中实际上是将在Stack上的值类型对象拷贝到Heap上,并以引用对象的存储格式保存,在这个过程中对所转换的引用类型对象进行修改操作,Stack上的值类型对象是不会变化的。而拆箱是将引用对象的值在拷贝回Stack上。

下面我们来看一个例子:

首先需要一个值类型,让它继承自一个接口

public interface IAdjustor

    {

        void Adjust();

    }

    public struct Size : IAdjustor

    {

        public int height;

        public int weight;

        public void Adjust()

        {

            height += 2;

            weight += 3;

        }

 }

然后,再用ClientApp测试一下:

class Program

    {

        static void Main(string[] args)

        {

            Size s = new Size();

            s.Adjust();

            Console.WriteLine("height:" + s.height + " weight:" + s.weight);

            IAdjustor itf = s;  //In box

            itf.Adjust();

            Console.WriteLine("height:" + s.height + " weight:" + s.weight);

            s = (Size)itf;      //Un box

            Console.WriteLine("height:" + s.height + " weight:" + s.weight);

            Console.Read();

        }

 }

    输出结果如下:

height:2 weight:3

height:2 weight:3

height:4 weight:6

从结果不难看出,在装箱后IAdjustor itf = s,调用已为引用对象的Adjust()方法,相应的值类型对象并没有发生变化。在拆箱后s = (Size)itf;,值类型对象就变化了