C# 0 The Complete Reference


prop , and a property called  MyProp



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

prop

, and a property called 



MyProp

 that manages access to 



prop

. As explained, a property 

by itself does not define a storage location. Instead, most properties simply manage access 

to a field. Furthermore, because 



prop

 is private, it can be accessed 



only

 through 



MyProp

.

The property 



MyProp

 is specified as 



public

 so it can be accessed by code outside of its 

class. This makes sense because it provides access to 

prop

, which is private. The 



get

 accessor 

simply returns the value of 

prop

. The 


set

 accessor sets the value of 



prop

 if and only if that 

value is positive. Thus, the 

MyProp

 property controls what values 



prop

 can have. This is 

the essence of why properties are important.

The type of property defined by 



MyProp

 is called a read-write property because it allows 

its underlying field to be read and written. It is possible, however, to create read-only and 

write-only properties. To create a read-only property, define only a 



get

 accessor. To define a 

write-only property, define only a 

set

 accessor.

You can use a property to further improve the fail-soft array class. As you know, all 

arrays have a 



Length

 property associated with them. Up to now, the 



FailSoftArray

 class 


simply used a public integer field called 

Length

 for this purpose. This is not good practice, 

though, because it allows 

Length

 to be set to some value other than the length of the fail-

soft array. (For example, a malicious programmer could intentionally corrupt its value.) We 

can remedy this situation by transforming 



Length

 into a read-only property, as shown in 

the following version of 

FailSoftArray

:

// Add Length property to FailSoftArray.



using System;

class FailSoftArray {

  int[] a; // reference to underlying array

  int len; // length of array -- underlies Length property

  public bool ErrFlag; // indicates outcome of last operation

  // 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;

    }

  }


www.freepdf-books.com


Download 4,07 Mb.

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