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

推荐订阅源

有赞技术团队
有赞技术团队
量子位
B
Blog RSS Feed
Schneier on Security
Schneier on Security
L
LINUX DO - 最新话题
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google DeepMind News
Google DeepMind News
N
News | PayPal Newsroom
阮一峰的网络日志
阮一峰的网络日志
Microsoft Security Blog
Microsoft Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
PCI Perspectives
PCI Perspectives
aimingoo的专栏
aimingoo的专栏
D
Docker
T
The Exploit Database - CXSecurity.com
Last Week in AI
Last Week in AI
W
WeLiveSecurity
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
J
Java Code Geeks
O
OpenAI News
C
Cisco Blogs
Hacker News - Newest:
Hacker News - Newest: "LLM"
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Help Net Security
Help Net Security
Scott Helme
Scott Helme
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
V
V2EX
P
Proofpoint News Feed
Simon Willison's Weblog
Simon Willison's Weblog
A
About on SuperTechFans
S
Securelist
G
Google Developers Blog
Cyberwarzone
Cyberwarzone
The GitHub Blog
The GitHub 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;,值类型对象就变化了