C# 0 The Complete Reference



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

get

 and 


set

accessors. The accessors are used to get and set the value of a variable. The key benefit of a 

property is that its name can be used in expressions and assignments like a normal variable, 

but in actuality the 



get

 and 


set

 accessors are automatically invoked. This is similar to the 

way that an indexer’s 

get

 and 


set

 accessors are automatically used.

The general form of a property is shown here:

type name

 {

    get {



         // get accessor code

    }


www.freepdf-books.com


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  

255


PART IPART I

    set {

        // set accessor code

    }


}

Here, 


type

 specifies the type of the property, such as 



int

, and 


name

 is the name of the property. 

Once the property has been defined, any use of 

name

 results in a call to its appropriate 

accessor. The 

set

 accessor automatically receives a parameter called 



value

 that contains the 

value being assigned to the property.

It is important to understand that properties do not define storage locations. Instead, a 

property typically manages access to a field. It does not, itself, provide that field. The field 

must be specified independently of the property. (The exception is the 



auto-implemented

property added by C# 3.0, which is described shortly.)

Here is a simple example that defines a property called 

MyProp

, which is used to access 

the field 

prop

. In this case, the property allows only positive values to be assigned.

// A simple property example.

using System;

class SimpProp {

  int prop; // field being managed by MyProp

  public SimpProp() { prop = 0; }

  /* This is the property that supports access to

     the private instance variable prop. It

     allows only positive values. */

  public int MyProp {

    get {

      return prop;

    }


    set {

      if(value >= 0) prop = value;

    }

  }


}

// Demonstrate a property.

class PropertyDemo {

  static void Main() {

    SimpProp ob = new SimpProp();

    Console.WriteLine("Original value of ob.MyProp: " + ob.MyProp);

    ob.MyProp = 100; // assign value

    Console.WriteLine("Value of ob.MyProp: " + ob.MyProp);

    // Can't assign negative value to prop.

    Console.WriteLine("Attempting to assign -10 to ob.MyProp");

    ob.MyProp = -10;

    Console.WriteLine("Value of ob.MyProp: " + ob.MyProp);

  }

}

www.freepdf-books.com




256

 

P a r t   I :  



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

Output from this program is shown here:

Original value of ob.MyProp: 0

Value of ob.MyProp: 100

Attempting to assign -10 to ob.MyProp

Value of ob.MyProp: 100

Let’s examine this program carefully. The program defines one private field, called 


Download 4,07 Mb.

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