C# 0 The Complete Reference


PART I C h a p t e r   1 0



Download 4,07 Mb.
Pdf ko'rish
bet350/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   346   347   348   349   350   351   352   353   ...   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  

265


PART IPART I

  int lowerBound; // smallest index

  int upperBound; // largest index

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

  // Construct array given its size.

  public RangeArray(int low, int high) {

    high++;

    if(high <= low) {

      Console.WriteLine("Invalid Indices");

      high = 1; // create a minimal array for safety

      low = 0;

    }

    a = new int[high - low];



    Length = high - low;

    lowerBound = low;

    upperBound = --high;

  }


  // This is the indexer for RangeArray.

  public int this[int index] {

    // This is the get accessor.

    get {

      if(ok(index)) {

        Error = false;

        return a[index - lowerBound];

      } else {

        Error = true;

        return 0;

      }

    }


    // This is the set accessor.

    set {

      if(ok(index)) {

        a[index - lowerBound] = value;

        Error = false;

      }


      else Error = true;

    }


  }

  // Return true if index is within bounds.

  private bool ok(int index) {

    if(index >= lowerBound & index <= upperBound) return true;

    return false;

  }


}

www.freepdf-books.com




266

 

P a r t   I :  



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

// Demonstrate the index-range array.

class RangeArrayDemo {

  static void Main() {

    RangeArray ra = new RangeArray(-5, 5);

    RangeArray ra2 = new RangeArray(1, 10);

    RangeArray ra3 = new RangeArray(-20, -12);

    // Demonstrate ra.

    Console.WriteLine("Length of ra: " + ra.Length);

    for(int i = -5; i <= 5; i++)

      ra[i] = i;

    Console.Write("Contents of ra: ");

    for(int i = -5; i <= 5; i++)

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

    Console.WriteLine("\n");

    // Demonstrate ra2.

    Console.WriteLine("Length of ra2: " + ra2.Length);

    for(int i = 1; i <= 10; i++)

      ra2[i] = i;

    Console.Write("Contents of ra2: ");

    for(int i = 1; i <= 10; i++)

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

    Console.WriteLine("\n");

    // Demonstrate ra3.

    Console.WriteLine("Length of ra3: " + ra3.Length);

    for(int i = -20; i <= -12; i++)

      ra3[i] = i;

    Console.Write("Contents of ra3: ");

    for(int i = -20; i <= -12; i++)

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

    Console.WriteLine("\n");

  }


}

The output from the program is shown here:

Length of ra: 11

Contents of ra: -5 -4 -3 -2 -1 0 1 2 3 4 5

Length of ra2: 10

Contents of ra2: 1 2 3 4 5 6 7 8 9 10

Length of ra3: 9

Contents of ra3: -20 -19 -18 -17 -16 -15 -14 -13 -12

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   346   347   348   349   350   351   352   353   ...   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