C# 0 The Complete Reference


Indexers Do Not Require an Underlying Array



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

Indexers Do Not Require an Underlying Array

It is important to understand that there is no requirement that an indexer actually operate 

on an array. It simply must provide functionality that appears “array-like” to the user of the 

indexer. For example, the following program has an indexer that acts like a read-only array 

that contains the powers of 2 from 0 to 15. Notice, however, that no actual array exists. 

Instead, the indexer simply computes the proper value for a given index.

// Indexers don't have to operate on actual arrays.

using System;

class PwrOfTwo {

  /* Access a logical array that contains

     the powers of 2 from 0 to 15. */

  public int this[int index] {

    // Compute and return power of 2.

    get {

      if((index >= 0) && (index < 16)) return pwr(index);

      else return -1;

    }

    // There is no set accessor.



  }

  int pwr(int p) {

    int result = 1;

    for(int i=0; i < p; i++)

      result *= 2;

    return result;

  }

}

www.freepdf-books.com




252

 

P a r t   I :  



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

class UsePwrOfTwo {

  static void Main() {

    PwrOfTwo pwr = new PwrOfTwo();

    Console.Write("First 8 powers of 2: ");

    for(int i=0; i < 8; i++)

      Console.Write(pwr[i] + " ");

    Console.WriteLine();

    Console.Write("Here are some errors: ");

    Console.Write(pwr[-1] + " " + pwr[17]);

    Console.WriteLine();

  }


}

The output from the program is shown here:

First 8 powers of 2: 1 2 4 8 16 32 64 128

Here are some errors: -1 -1

Notice that the indexer for 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   328   329   330   331   332   333   334   335   ...   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