C# 0 The Complete Reference


PART I C h a p t e r   1 0



Download 4,07 Mb.
Pdf ko'rish
bet347/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   343   344   345   346   347   348   349   350   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 0 :  

I n d e x e r s   a n d   P r o p e r t i e s  

263


PART IPART I

class FailSoftArray {

  int[] a; // reference to underlying array

  // Construct array given its size.

  public FailSoftArray(int size) {

    a = new int[size];

    Length = size;

  }


  // An auto-implemented, read-only Length property.

  public int Length { get; private set; }

  // An auto-implemented, read-only Error property.

  public bool Error { get; private set; }

  // This is the indexer for FailSoftArray.

  public int this[int index] {

    // This is the get accessor.

    get {

      if(ok(index)) {

        Error = false;

        return a[index];

      } else {

        Error = true;

        return 0;

      }

    }


    // This is the set accessor.

    set {

      if(ok(index)) {

        a[index] = value;

        Error = false;

      }


      else Error = true;

    }


  }

  // Return true if index is within bounds.

  private bool ok(int index) {

   if(index >= 0 & index < Length) return true;

   return false;

  }


}

// Demonstrate the improved fail-soft array.

class FinalFSDemo {

  static void Main() {

    FailSoftArray fs = new FailSoftArray(5);

    // Use Error property.

    for(int i=0; i < fs.Length + 1; i++) {

      fs[i] = i*10;

www.freepdf-books.com



264

 

P a r t   I :  



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

      if(fs.Error)

        Console.WriteLine("Error with index " + i);

    }


  }

}

This version of 



FailSoftArray

 works the same as the previous version, but it does not 

contain the explicitly declared backing fields.

Here are some restrictions that apply to using access modifiers with accessors. First, 

only the 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   343   344   345   346   347   348   349   350   ...   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