C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet529/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   525   526   527   528   529   530   531   532   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART IPART I

delegate bool IsEven(int v);

class SimpleLambdaDemo {

  static void Main() {

    // Create an Incr delegate instance that refers to

    // a lambda expression that increases its parameter by 2.

    Incr incr = count => count + 2;

    // Now, use the incr lambda expression.

    Console.WriteLine("Use incr lambda expression: ");

    int x = -10;

    while(x <= 0) {

      Console.Write(x + " ");

      x = incr(x); // increase x by 2

    }


    Console.WriteLine("\n");

    // Create an IsEven delegate instance that refers to

    // a lambda expression that returns true if its parameter

    // is even and false otherwise.

    IsEven isEven = n => n % 2 == 0;

    // Now, use the isEven lambda expression.

    Console.WriteLine("Use isEven lambda expression: ");

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

      if(isEven(i)) Console.WriteLine(i + " is even.");

  }


}

The output is shown here:

Use incr lambda expression:

-10 -8 -6 -4 -2 0

Use isEven lambda expression:

2 is even.

4 is even.

6 is even.

8 is even.

10 is even.

In the program, pay special attention to these declarations:

Incr incr = count => count + 2;

IsEven isEven = n => n % 2 == 0;

The first assigns to 



incr

 a lambda expression that returns the result of increasing the value 

passed to 

count

 by 2. This expression can be assigned to an 



Incr

 delegate because it is 

compatible with 

Incr

’s declaration. The argument used in the call to 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   525   526   527   528   529   530   531   532   ...   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