C# 0 The Complete Reference


interface . Thus,  the set



Download 4,07 Mb.
Pdf ko'rish
bet415/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   411   412   413   414   415   416   417   418   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

interface

. Thus, 


the

set

 accessor, for example, cannot be specified as private in an 



interface

.

Here is a rewrite of the 



ISeries

 interface and the 



ByTwos

 class that uses a property 

called

Next

 to obtain and set the next element in the series:

// Use a property in an interface.

using System;

public interface ISeries {

  // An interface property.

  int Next {

    get; // return the next number in series

    set; // set next number

  }


}

// Implement ISeries.

class ByTwos : ISeries {

  int val;

  public ByTwos() {

    val = 0;

  }

  // Get or set value.



  public int Next {

    get {

      val += 2;

      return val;

    }

    set {



      val = value;

    }


  }

}

// Demonstrate an interface property.



class SeriesDemo3 {

  static void Main() {

    ByTwos ob = new ByTwos();

    // Access series through a property.

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

      Console.WriteLine("Next value is " + ob.Next);

    Console.WriteLine("\nStarting at 21");

    ob.Next = 21;

www.freepdf-books.com



320

 

P a r t   I :  



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

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

      Console.WriteLine("Next value is " + ob.Next);

  }


}

The output from this program is shown here:

Next value is 2

Next value is 4

Next value is 6

Next value is 8

Next value is 10

Starting at 21

Next value is 23

Next value is 25

Next value is 27

Next value is 29

Next value is 31

Interface Indexers

An interface can specify an indexer. A one-dimensional indexer declared in an interface has 

this general form:

// interface indexer



element-type

 this[int 



index

] {


   get;

   set;


}

As before, only 



get

 or 


set

 will be present for read-only or write-only indexers, respectively. 

Also, no access modifiers are allowed on the accessors when an indexer is declared in an 

interface

.

Here is another version of 



ISeries

 that adds a read-only indexer that returns the 



i

-th


element in the series.

// Add an indexer in an interface.

using System;

public interface ISeries {

  // An interface property.

  int Next {

    get; // return the next number in series

    set; // set next number

  }

  // An interface indexer.



  int this[int index] {

    get; // return the specified number in series

  }

}

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   411   412   413   414   415   416   417   418   ...   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