C# 0 The Complete Reference


PART I C h a p t e r   1 8



Download 4,07 Mb.
Pdf ko'rish
bet661/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   657   658   659   660   661   662   663   664   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 8 :  

G e n e r i c s  

531


PART IPART I

// Demonstrate a generic interface.

using System;

public interface ISeries {

  T GetNext(); // return next element in series

  void Reset(); // restart the series

  void SetStart(T v); // set the starting element

}

// Implement ISeries.



class ByTwos : ISeries {

  T start;

  T val;

  // This delegate defines the form of a method

  // that will be called when the next element in

  // the series is needed.

  public delegate T IncByTwo(T v);

  // This delegate reference will be assigned the

  // method passed to the ByTwos constructor.

  IncByTwo incr;

  public ByTwos(IncByTwo incrMeth) {

    start = default(T);

    val = default(T);

    incr = incrMeth;

  }

  public T GetNext() {



    val = incr(val);

    return val;

  }

  public void Reset() {



    val = start;

  }


  public void SetStart(T v) {

    start = v;

    val = start;

  }


}

class ThreeD {

  public int x, y, z;

  public ThreeD(int a, int b, int c) {

    x = a;

    y = b;

    z = c;

  }


}

www.freepdf-books.com




532

 

P a r t   I :  



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

class GenIntfDemo {

  // Define plus two for int.

  static int IntPlusTwo(int v) {

    return v + 2;

  }


  // Define plus two for double.

  static double DoublePlusTwo(double v) {

    return v + 2.0;

  }


  // Define plus two for ThreeD.

  static ThreeD ThreeDPlusTwo(ThreeD v) {

    if(v==null) return new ThreeD(0, 0, 0);

    else return new ThreeD(v.x + 2, v.y + 2, v.z + 2);

  }

  static void Main() {



    // Demonstrate int series.

    ByTwos intBT = new ByTwos(IntPlusTwo);

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

      Console.Write(intBT.GetNext() + "  ");

    Console.WriteLine();

    // Demonstrate double series.

    ByTwos dblBT = new ByTwos(DoublePlusTwo);

    dblBT.SetStart(11.4);

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

      Console.Write(dblBT.GetNext() + "  ");

    Console.WriteLine();

    // Demonstrate ThreeD series.

    ByTwos ThrDBT = new ByTwos(ThreeDPlusTwo);

    ThreeD coord;

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

      coord = ThrDBT.GetNext();

      Console.Write(coord.x + "," +

                    coord.y + "," +

                    coord.z + "  ");

    }


    Console.WriteLine();

  }


}

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   657   658   659   660   661   662   663   664   ...   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