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

推荐订阅源

S
Secure Thoughts
S
Securelist
P
Proofpoint News Feed
D
DataBreaches.Net
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Project Zero
Project Zero
A
About on SuperTechFans
罗磊的独立博客
WordPress大学
WordPress大学
月光博客
月光博客
Latest news
Latest news
C
Cyber Attacks, Cyber Crime and Cyber Security
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
W
WeLiveSecurity
Attack and Defense Labs
Attack and Defense Labs
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
AI
AI
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Help Net Security
Help Net Security
T
Tor Project blog
V
Vulnerabilities – Threatpost
C
Cisco Blogs
I
Intezer
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MyScale Blog
MyScale Blog
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
Forbes - Security
Forbes - Security
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
博客园 - 叶小钗
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Simon Willison's Weblog
Simon Willison's Weblog
C
CERT Recently Published Vulnerability Notes
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic

博客园 - 昊子

工作流参考模型(Workflow Reference Model) DNN default document的异常错误 如何使用GoogleCode提供的SVN Source Control服务 Norton PartitionMagic 8.0 Resizing Boot Partition 智能提示和那些值得崇拜的人 推荐一个Flex & AIR皮肤站点 SessionDiskCache 0.1版发布 Flex locale Flex Repeater 多层嵌套 从没走远 SubmitMask 1.0 发布 如何获取Footer中的子控件 DNN学习笔记 之一 配置 使用NHibernate时产生的一个错误 NHibernate官方文档 之 NHibernate指南-前言 C#反转单向链表 C#事件编程 NHibernate和SqlImage 结束无谓的讨论吧
静态构造函数和静态成员变量初始化的调用时间
昊子 · 2006-06-14 · via 博客园 - 昊子

    class Class1
    {
        [STAThread]
        
static void Main(string[] args)
        {
            System.Console.WriteLine(anobject
== null);
            System.Console.WriteLine(
"程序开始");
        }
        
static Class1()
        {
            System.Console.WriteLine(
"静态构造函数调用");
        }
        
static readonly object anobject = new object();
    }

单步调试得出结论:静态成员初始化在静态构造函数之前,静态构造函数调用在静态方法前,而静态成员初始化又是在什么时候调用的呢?

    class Class1
    {
        [STAThread]
        
static void Main(string[] args)
        {
            System.Console.WriteLine(anobject
== null);
            System.Console.WriteLine(
"程序开始");
            Class2 c2 
= new Class2();
            c2.Do();
            Class2.StaticDo();

        }

static Class1()
        {
            System.Console.WriteLine(
"静态构造函数调用");
        }
        
static readonly object anobject = new object();
    }
public class Class2
    {
        
static Class2()
        {
            System.Console.WriteLine(
"Class2静态构造函数");
        }
        
public Class2()
        {

        }

public void Do()
        {
            System.Console.WriteLine(
"Class2实例.Do()");
        }
        
public static object Something = new object();public static void StaticDo()
        {
            System.Console.WriteLine(
"Class2静态方法DO");
        }
    }

再次跟踪调试。顺序如下,类被首次提到(实例化或调用静态对象/方法)时开始执行类的静态成员初始化,初始化后如果存在静态构造函数则调用静态构造函数,之后进行相应操作。
那么多个静态成员按什么顺序初始化呢

        public static object Something3 = new object();
        
public static object Something1 = new object();
        
public static object Something2 = new object();

顺序3-1-2,也就是编译顺序
这个顺序不知道会不会因为调用有所改变

    public class Class2
    {
        
public static object Something3 = Something1;
        
public static object Something1 = new object();
        
public static object Something2 = new object();
    }
class Class1
    {
        [STAThread]
        
static void Main(string[] args)
        {
            System.Console.WriteLine(Class2.Something3 
== null);
        }
    }

结果Something3的值为null,也就是说不会因为先调用而先初始化。