English 中文(简体)
C# 2级,固定成员相互参照
原标题:C# two classes with static members referring to each other

我不禁要问,这一法典为何没有结束无休止的入侵。 我猜测,它把固定成员的自动初始化与默认的数值联系起来,但有人可以告诉我:一步一步”<>?

public class A
{
    public static int a = B.b + 1;
}
public class B
{
    public static int b = A.a + 1;
}

static void Main(string[] args)
{
    Console.WriteLine("A.a={0}, B.b={1}", A.a, B.b); //A.a=2, B.b=1
    Console.Read();
}
最佳回答

我要说的是:

  • A.a is queried, which causes the A static initializer to fire
  • This accesses B.b, causing the B static initializer to fire
  • A.a is queried; the type initializer is already activated (but no assignment has yet occurred), so the field (not yet assigned) is read as 0
  • 0 + 1 is 1, which is assigned to B.b <===========================
  • we now exit the B cctor and go back to the A cctor
  • 1 + 1 is 2, which is assigned to A.a <===========================
  • we now exit the A cctor
  • 2 is returned (WriteLine) for A.a
  • we query (on WriteLine) B.b; the cctor has already fired so we see 1
问题回答

Marc是正确的。 我只想补充他的回答,即你的问题用具体写法第10.5.1节回答。

The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. If a static constructor exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.

通知最后一点。 具体来说,你想引用你的具体例子,说明哪一种命令是允许的;所有具体保障是,实地的初始者按文字顺序在<><><>之前>完成。 固定构件运行。 www.un.org/Depts/DGACM/index_spanish.htm 它不能保证在另一类田地之前或之后开始采用一种类型的田地。

例如,jit汇编者可以说:“我看到,A类和B类首次用于这一即将被切断的方法,请允许我借此机会确保这些类型的装载......”。 垃圾处理厂目前可以执行现场初步设计器,并且可以首先自行选择。

简言之,(1)you不能依靠这一行为;该行为是执行界定的;(2) 具体回答你的准确问题;在你对语言语语语语语语质问问时考虑具体内容。

It has to do with the order in which you access the static properties. The first evaluated is A.a. When evaluating A.a, B.b gets initialized. Since the actual assignment to a is not finished, the value of a remains 0, thus B.b becomes 1. After B.b is initialized, the value can be assigned to A.a, that is 1+1, thus 2

第一种装载类型是<代码>。 A。 因此,这些类型已装满,其固定成员为<代码>a,其违约值为零。 之后,打电话到<代码>A。 这一构造参考资料类型B,如B,也装上了,静态构造也叫上。 该建筑商反过来将参考类别<代码>A,但A已经装载,因此此处无所作为;<代码>b使其价值为零(现值<代码>a>)加一(现值)。 之后,计算出<代码>B回归和<代码>a数值的固定构造。

有趣的是,我修改了你抽样守则中的产出顺序:

    Console.WriteLine("B.b={0} A.a={1}", B.b, A.a);

我得出相反的结果:

B.b=2 A.a=1

因此,它想到的是,要与他们接触的顺序有关。

因此,鉴于产出可能会发生变化,增加早期使用其中一种变量,因此,它似乎像这种重新界定的数值一样,是A BAD IDEA(TM) :-

由于A.a首先在康塞洛省提及。 书写器,其装货量首先导致B装载A.a的数值为0 =>B.b = 1 =>A.a 变成2

反转录印刷和看着它的情况。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...