C# Language Specification


Static field initialization



Download 0,75 Mb.
bet360/524
Sana06.01.2022
Hajmi0,75 Mb.
#324842
1   ...   356   357   358   359   360   361   362   363   ...   524
Bog'liq
CSharp Language Specification

Static field initialization


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 (§10.12) 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. The example

using System;

class Test
{
static void Main() {
Console.WriteLine("{0} {1}", B.Y, A.X);
}

public static int F(string s) {


Console.WriteLine(s);
return 1;
}
}

class A
{


public static int X = Test.F("Init A");
}

class B
{


public static int Y = Test.F("Init B");
}

might produce either the output:

Init A
Init B
1 1

or the output:

Init B
Init A
1 1

because the execution of X’s initializer and Y’s initializer could occur in either order; they are only constrained to occur before the references to those fields. However, in the example:

using System;

class Test


{
static void Main() {
Console.WriteLine("{0} {1}", B.Y, A.X);
}

public static int F(string s) {


Console.WriteLine(s);
return 1;
}
}

class A
{


static A() {}

public static int X = Test.F("Init A");


}

class B
{


static B() {}

public static int Y = Test.F("Init B");


}

the output must be:

Init B
Init A
1 1

because the rules for when static constructors execute (as defined in §10.12) provide that B’s static constructor (and hence B’s static field initializers) must run before A’s static constructor and field initializers.



        1. Download 0,75 Mb.

          Do'stlaringiz bilan baham:
1   ...   356   357   358   359   360   361   362   363   ...   524




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish