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

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
美团技术团队
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
H
Help Net Security
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
博客园_首页
A
About on SuperTechFans
MongoDB | Blog
MongoDB | Blog
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
B
Blog
The Register - Security
The Register - Security
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
Last Week in AI
Last Week in AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
Jina AI
Jina AI
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News - Newest:
Hacker News - Newest: "LLM"
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
W
WeLiveSecurity
Latest news
Latest news
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
L
Lohrmann on Cybersecurity
博客园 - Franky
Recent Commits to openclaw:main
Recent Commits to openclaw:main
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 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;,值类型对象就变化了