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

推荐订阅源

Google Online Security Blog
Google Online Security Blog
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
V
V2EX
雷峰网
雷峰网
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
F
Full Disclosure
Y
Y Combinator Blog
V
V2EX - 技术
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
SecWiki News
SecWiki News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
量子位
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
K
Kaspersky official blog
B
Blog
A
Arctic Wolf
Hacker News: Ask HN
Hacker News: Ask HN
L
LangChain Blog
T
Tor Project blog
P
Privacy & Cybersecurity Law Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
Lohrmann on Cybersecurity
D
Docker
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy International News Feed
Simon Willison's Weblog
Simon Willison's Weblog

博客园 - 风前絮~~

回来啦 Rainbow分页解决方案 FTB2.0和CuteEditor的一些问题 testFTB2.0 testCuteEditor 看看MS内部对.NET的使用情况... base想到... 多个Main函数的应用程序 - 风前絮~~ - 博客园 伟大架构师的秘密 权限管理越来越复杂 ASP.NET跨应用程序进行登录的解决 Server的Transfer和Response的Redirect FreeTextBox实现机制 FreeTextBox的ToolbarButton整理 Testing vs Debugger C#中"is" vs "as" - 风前絮~~ 再比较动态调用代码 .NET建议使用的大小写命名原则 using的几种用法
稍不留神产生代码垃圾
风前絮~~ · 2004-09-16 · via 博客园 - 风前絮~~

       一个类型允许定义多个实例构造器,在使用过程中确实是十分方便的。但是,在定义这些构造器时,如果稍不留神,可能就使你的代码编译后产生了好多不必要的垃圾,增加了程序集的大小,也不够简洁。

例如:

using System;

namespace testConstruct
{
    
/// <summary>
    
/// Class2 的摘要说明。
    
/// </summary>

    public class Class2
    
{
        Int32 x 
= 6;
        String s 
= "Hello";
        Double d 
= 3.24;
        Byte b;
        
public Class2()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
public Class2(Int32 x)
        
{

        }


        
public Class2(String s)
        
{

        }

    }

}

 用ILDASM来看生成的IL结果:

        可以看到,三个构造类都重复初始化了几个变量,造成编译后程序集大小的增加。
就三个构造函数已经占了40+40+40=120 Bytes.

如果稍微修改一下,如下面所示:

using System;

namespace testConstruct
{
    
/// <summary>
    
/// Class3 的摘要说明。
    
/// </summary>

    public class Class3
    
{
        Int32 x 
= 6;
        String s 
= "Hello";
        Double d 
= 3.24;
        Byte b;
        
public Class3()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
public Class3(Int32 x) : this()
        
{

        }


        
public Class3(String s) : this()
        
{

        }


    }

}


再用ILDASM来看产生的IL结果:

可见生成的程序集大小确实减少了不少。现在三个构造函数才占了40+7+7=54 Bytes.

就三个构造函数的代码而言,大小缩减了一半不止。