C# 0 The Complete Reference


static  method.  However, the use of  Val



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

static

 method. 

However, the use of 

Val

 is okay since it is a 



static

 variable.

The same problem occurs when trying to call a non-

static

 method from within a 



static

method of the same class. For example:

using System;

class AnotherStaticError {

  // A non-static method.

  void NonStaticMeth() {

     Console.WriteLine("Inside NonStaticMeth().");

  }


  /* Error! Can't directly call a non-static method

     from within a static method. */

  static void staticMeth() {

    NonStaticMeth(); // won't compile

  }

}

In this case, the attempt to call a non-



static

 (that is, instance method) from a 



static

 method 


causes a compile-time error.

It is important to understand that a 



static

 method 


can

 call instance methods and access 

instance variables of its class if it does so through an object of that class. It is just that it 

cannot use an instance variable or method without an object qualification. For example, 

this fragment is perfectly valid:

class MyClass {

  // A non-static method.

  void NonStaticMeth() {

     Console.WriteLine("Inside NonStaticMeth().");

  }


  /* Can call a non-static method through an

     object reference from within a static method. */

www.freepdf-books.com



208

 

P a r t   I :  



T h e   C #   L a n g u a g e

  public static void staticMeth(MyClass ob) {

    ob.NonStaticMeth(); // this is OK

  }


}

Here, 


NonStaticMeth( )

 is called by 



staticMeth( )

 through 



ob

, which is an object of type 



MyClass

.

Because



static

 fields are independent of any specific object, they are useful when you 

need to maintain information that is applicable to an entire class. Here is an example of 

such a situation. It uses a 



static

 field to maintain a count of the number of objects that are 

in existence.

// Use a static field to count instances.

using System;

class CountInst {

  static int count = 0;

  // Increment count when object is created.

  public CountInst() {

    count++;

  }

  // Decrement count when object is destroyed.



  ~CountInst() {

    count--;

  }

  public static int GetCount() {



    return count;

  }


}

class CountDemo {

  static void Main() {

    CountInst ob;

    for(int i=0; i < 10; i++) {

      ob = new CountInst();

      Console.WriteLine("Current count: " + CountInst.GetCount());

    }


  }

}

The output is shown here:



Current count: 1

Current count: 2

Current count: 3

Current count: 4

Current count: 5

Current count: 6

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   281   282   283   284   285   286   287   288   ...   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