静态字段初始化顺序(C#) - 有人可以解释这个片段? [英] Static field initialization order (C#) - can someone explain this snippet?

查看:214
本文介绍了静态字段初始化顺序(C#) - 有人可以解释这个片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个C ++程序员学习C#。 。我目前正在读C#4.0果壳中的



我来翻过这一声明/ snipet 74页:




在该领域是

顺序执行静态字段初始声明。下面的例子
说明本:X初始化为
0和Y初始化为3




 类Foo 
{
公共静态INT X = Y; // 0
公共静态INT Y = 3; // 3
}



我不明白怎么X可以Y轴分配的价值,无Y首先被宣布。 ?我失去了一些东西在这里。



顺便说一句,从C来++背景,我倾向于使用构造术语构造函数 - 但是,我还没来翻过在C#中的术语 - 这个术语构造函数在C#世界也用




$? b $ b

在同一页上的另一个例子(前面提到的那本书)是这样的:

 类节目
{
静态无效的主要(){Console.WriteLine(Foo.X); } // 3
}
类Foo
{
公共静态美孚实例=新的Foo();
公共静态INT X = 3;
美孚(){Console.WriteLine(X); } // 0
}



这本书状态(上面的例子):




该示例打印其次是3
0,因为字段初始值是
实例化一个Foo前X
执行初始化为3:




我有一些进一步的问题WRT的例子。




  1. 这两个例子都出现在部分标题为静态构造函数和现场初始化顺序然而,代码示例不显示静态构造函数 - ATLEAST,而不是一个,我可以很容易地识别。我期待一个静态构造函数具有相同的名称作为类,是无参数和由'的静态的'关键字来preceeded。所以,我没有看到的例子如何与部分的标题。缺少什么我在这里


  2. 在第二个例子中,(非静态)构造函数打印出X的值 - 其中有明确地被分配的3前行的价值 - ?!然而,打印输出为0。为什么



解决方案

在C#中,原始类型,如 INT 取默认值。对于 INT ,缺省值为0。这并不像C ++,你必须初始化值或将越来越从内存中随机脏值。由于Y被称为是类型 INT ,X可以分配它的缺省值0值。



据作为关键字男星,我看不出它曾经很为C#程序员,但我很熟悉的名词。我相信这是在Visual Studio少数地方使用,例如在对象浏览器。


I am a C++ programmer learning C#. I am currently reading C#4.0 in a Nutshell.

I have come accross this statement/snipet on page 74:

Static field initializers run in the order in which the fields are declared. The following example illustrates this: X is initialized to 0 and Y is initialized to 3.

class Foo
{
    public static int X = Y; // 0
    public static int Y = 3; // 3
}

I dont understand how X can be assigned the value in Y, without Y being first declared. Am I missing something here?

As an aside, coming from a C++ background, I tend to use the term ctor for constructor - however, I haven't yet come accross the term in C# - is the term ctor also used in the C# world?

[Edit]

A further example on the same page (in the book mentioned earlier) is this:

class Program
{
    static void Main() { Console.WriteLine (Foo.X); } // 3
}
class Foo
{
    public static Foo Instance = new Foo();
    public static int X = 3;
    Foo() { Console.WriteLine (X); } // 0
}

The book states (for the example above):

The example prints 0 followed by 3 because the field initializer that instantiates a Foo executes before X is initialized to 3:

I have some further questions w.r.t the examples.

  1. Both examples appear under the section entitled Static constructors and field initialization order however, the code examples dont show a static ctor - atleast, not one that I can easily recognise. I was expecting a static ctor to have the same name as the class, be parameterless and be preceeded by the 'static' keyword. So I dont see how the examples relate to the section heading. What am I missing here?

  2. In the second example, the (non static) ctor prints out the value of X - which had explicitly been assigned the value of 3 in the previous line - and yet, the output printed out is 0. Why?!

解决方案

In C#, primitive types such as int take on a default value. For int, the default value is 0. This is not like C++ where you have to initialize the value or it will be getting a random dirty value from memory. Since Y is known to be of type int, X can be assigned it's default value of 0.

As far as the keyword ctor, I don't see it used very much as a C# programmer, but I am familiar with the term. I believe it is used in a few places in Visual Studio, for instance in the object browser.

这篇关于静态字段初始化顺序(C#) - 有人可以解释这个片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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