使用静态构造函数(乔恩斯基特谜题) [英] Using Static Constructor (Jon Skeet Brainteaser)

查看:158
本文介绍了使用静态构造函数(乔恩斯基特谜题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个相对新手我尝试阅读,就像我可以了解一个特定的主题和测试/编写大量的代码,我可以。我一直在寻找的容斯脑筋急转弯(问题#2)之一,我的输出比答案不同。这使得让我在这里问,如果事情的最新版本,并看看有什么输出其他从该代码得到了改变。

As a relative newbie I try to read as much as I can about a particular subject and test/write as much code as I can. I was looking at one of Jons Brainteasers (question #2) and my output was different than the answer. Which makes brings me here to ask if something has changed in recent versions and to see what output others are getting from this code.

现在的问题是,什么将显示,为什么,你是如何有信心

The question is, "What will be displayed, why, and how confident are you?"

using System;

class Foo
{
    static Foo()
    {
        Console.WriteLine ("Foo");
    }
}

class Bar
{
    static int i = Init();

    static int Init()
    {
        Console.WriteLine("Bar");
        return 0;
    }
}

class Test
{
    static void Main()
    {
        Foo f = new Foo();
        Bar b = new Bar();
    }
}

什么,如果有的话,会导致我们得到两个不同的答案?

What, if anything, would cause us to get two different answers?

推荐答案

现在尝试在释放模式,调试器;-p

Now try it in release mode, outside of the debugger ;-p

我得到/不同的结果没有一个调试器。调试器打乱了很多细微差别/的优化,所以我只能猜测,这是那个时代当调试问题之一。这使它更难调试;-p

I get different results with/without a debugger. The debugger upsets a lot of subtle nuances / optimisations, so I can only guess this is one of those times where the debugger matters. Which makes it even harder to debug ;-p

这篇关于使用静态构造函数(乔恩斯基特谜题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆