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

推荐订阅源

Vercel News
Vercel News
Simon Willison's Weblog
Simon Willison's Weblog
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
S
SegmentFault 最新的问题
博客园 - 司徒正美
The GitHub Blog
The GitHub Blog
量子位
SecWiki News
SecWiki News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Palo Alto Networks Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
爱范儿
爱范儿
S
Secure Thoughts
G
Google Developers Blog
Microsoft Security Blog
Microsoft Security Blog
D
Docker
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
F
Fortinet All Blogs
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
Recorded Future
Recorded Future
罗磊的独立博客
Forbes - Security
Forbes - Security
Security Latest
Security Latest
NISL@THU
NISL@THU
T
The Exploit Database - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
T
Tenable Blog
C
Cybersecurity and Infrastructure Security Agency CISA
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
K
Kaspersky official blog
月光博客
月光博客
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - 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-07-11 · via 博客园 - KiddLee

原来写了一篇有关装箱拆箱的文章,这几天看到这个例子,觉得更能说明一些问题,首先看下面的例子

namespace InBoxUnInBoxTest

{

    internal struct Point

    {

        private int m_x, m_y;

        public Point(int x, int y)

        {

            m_x = x;

            m_y = y;

        }

        public void Change(int x, int y)

        {

            m_x = x;

            m_y = y;

        }

        public override string ToString()

        {

            return String.Format("({0},{1})", m_x, m_y);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Point p = new Point(1, 1);

            Console.WriteLine(p);

            p.Change(2, 2);

            Console.WriteLine(p);

            object o = p;

            Console.WriteLine(o);

            ((Point)o).Change(3, 3);

            Console.WriteLine(o);

        }

    }

}

其结果是:

(1,1)

(2,2)

(2,2)

(2,2)

    前三个输出没有什么问题,来说说第四个输出:o 是一个 Object 类型,首先进行的是拆箱的操作。这时系统就要把 o 复制到一堆栈上的一个临时空间中。对象 o 本身还是存储在 托管对上的,所以这是调用 Change 函数对于 o 来说没有起任何作用。所以才出现了上述结果。

      再来看看下面的程序:

namespace InBoxTest

{

    internal interface IChangeBoxedPoint

    {

        void Change(int x, int y);

    }

    internal struct Point : IChangeBoxedPoint

    {

        private int m_x, m_y;

        public Point(int x, int y)

        {

            m_x = x;

            m_y = y;

        }

        public void Change(int x, int y)

        {

            m_x = x;

            m_y = y;

        }

        public override string ToString()

        {

            return String.Format("({0},{1})", m_x, m_y);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Point p = new Point(1, 1);

            Console.WriteLine(p);

            p.Change(2, 2);

            Console.WriteLine(p);

            object o = p;

            Console.WriteLine(o);

            ((Point)o).Change(3, 3);

            Console.WriteLine(o);

            ((IChangeBoxedPoint)p).Change(4, 4);

            Console.WriteLine(p);

            ((IChangeBoxedPoint)o).Change(5, 5);

            Console.WriteLine(o);

            Console.Read();

        }

    }

}

其结果是:

(1,1)

(2,2)

(2,2)

(2,2)

(2,2)

(5,5)

      主要来看看最后两个结果:在这个程序中定义了一个接口,自定义的结构继承自这个类型。第五个结果输出的是(2,2),程序中进行的是装箱处理,这样 p 就以引用类型存储形势被复制到托管对上,这样在进行修改,实际上只是修改托管对上的对象。 p 本身没有变化。如果我们再看看托管对上的对象是否进行修改了,加入如下代码测试一下:

      IChangeBoxedPoint test = (IChangeBoxedPoint)p;

       test.Change(4, 4);

       Console.WriteLine(p);

       Console.WriteLine(test);

其结果:(4,4)显示出来了

      然后再来看看最后一个结果,系统对对象 o 实际上就是强制类型的转换,并不是装箱拆箱的操作。所以操作的还是 o 本身。

      最后说一句:装拆箱要在堆栈和托管对上进行对象复制(这当中包括值类型和引用类型存储形式的变化),性能和安全性降低,所以在系统开发中不应该大量使用,当然我想可以尽量使用泛形来解决问题。