C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet287/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   283   284   285   286   287   288   289   290   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Static Constructors

A constructor can also be specified as 



static

. A 


static

 constructor is typically used to 

initialize features that apply to a class rather than an instance. Thus, it is used to initialize 

aspects of a class before any objects of the class are created. Here is a simple example:

// Use a static constructor.

using System;

class Cons {

  public static int alpha;

  public int beta;

  // A static constructor.

  static Cons() {

    alpha = 99;

    Console.WriteLine("Inside static constructor.");

  }


  // An instance constructor.

  public Cons() {

    beta = 100;

    Console.WriteLine("Inside instance constructor.");

  }

}

class ConsDemo {



  static void Main() {

    Cons ob = new Cons();

    Console.WriteLine("Cons.alpha: " + Cons.alpha);

    Console.WriteLine("ob.beta: " + ob.beta);

  }

}

Here is the output:



Inside static constructor.

Inside instance constructor.

Cons.alpha: 99

ob.beta: 100

Notice that the 

static

 constructor is called automatically (when the class is first loaded) and 

before the instance constructor. This can be generalized. In all cases, the 

static

 constructor 

will be executed before any instance constructor. Furthermore, 

static

 constructors cannot 

have access modifiers (thus, they use default access) and cannot be called by your program.

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   283   284   285   286   287   288   289   290   ...   1096




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