C# 0 The Complete Reference


len  for its storage.  Length



Download 4,07 Mb.
Pdf ko'rish
bet338/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   334   335   336   337   338   339   340   341   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

len

 for its storage. 



Length

 defines 

only a 

get

 accessor, which means that it is read-only. Thus, 



Length

 can be read, but not 

www.freepdf-books.com



258

 

P a r t   I :  



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

changed. To prove this to yourself, try removing the comment symbol preceding this line 

in the program:

// fs.Length = 10; // Error, illegal!

When you try to compile, you will receive an error message stating that 

Length

 is read-only.

Although the addition of the 

Length

 property improves 



FailSoftArray

, it is not the only 

improvement that properties can make. The 

ErrFlag

 member is also a prime candidate for 

conversion into a property since access to it should also be limited to read-only. Here is the 

final improvement of 



FailSafeArray

. It creates a property called 



Error

 that uses the original 



ErrFlag

variable as its storage, and 



ErrFlag

 is made private to 



FailSoftArray

.

// Convert ErrFlag into a property.



using System;

class FailSoftArray {

  int[] a; // reference to underlying array

  int len; // length of array

  bool ErrFlag; // now private

  // Construct array given its size.

  public FailSoftArray(int size) {

    a = new int[size];

    len = size;

  }


  // Read-only Length property.

  public int Length {

    get {

      return len;

    }

  }


  // Read-only Error property.

  public bool Error {

    get {

      return ErrFlag;

    }

  }


  // This is the indexer for FailSoftArray.

  public int this[int index] {

    // This is the get accessor.

    get {

      if(ok(index)) {

        ErrFlag = false;

        return a[index];

      } else {

        ErrFlag = true;

        return 0;

      }

    }


www.freepdf-books.com


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   334   335   336   337   338   339   340   341   ...   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