C# 0 The Complete Reference



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

r

, uses a 



for

loop, and has a 



return

 statement. These are legal inside a statement lambda. In essence, a 

statement lambda closely parallels an anonymous method. Therefore, many anonymous 

methods will be converted to statement lambdas when updating legacy code. (As mentioned, 

as of C# 3.0, lambda expressions are the preferred way of creating anonymous functions.) 

One other point: When a 



return

 statement occurs within a lambda expression, it simply 

causes a return from the lambda. It does not cause the enclosing method to return.

Before concluding, it is worthwhile to see another example that shows the statement 

lambda in action. The following program reworks the first delegate example in this chapter 

so it uses statement lambdas (rather than standalone methods) to accomplish various string 

modifications:

// The first delegate example rewritten to use

// statement lambdas.

using System;

// Declare a delegate type.

delegate string StrMod(string s);

www.freepdf-books.com



418

 

P a r t   I :  



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

class UseStatementLambdas {

  static void Main() {

    // Create delegates that refer to lambda expressions

    // that perform various string modifications.

    // Replaces spaces with hyphens.

    StrMod ReplaceSpaces = s => {

             Console.WriteLine("Replacing spaces with hyphens.");

             return s.Replace(' ', '-');

           };

    // Remove spaces.

    StrMod RemoveSpaces = s => {

             string temp = "";

             int i;

             Console.WriteLine("Removing spaces.");

             for(i=0; i < s.Length; i++)

               if(s[i] != ' ') temp += s[i];

             return temp;

           };

    // Reverse a string.

    StrMod Reverse = s => {

             string temp = "";

             int i, j;

             Console.WriteLine("Reversing string.");

             for(j=0, i=s.Length-1; i >= 0; i--, j++)

             temp += s[i];

             return temp;

           };

    string str;

    // Call methods through the delegate.

    StrMod strOp = ReplaceSpaces;

    str = strOp("This is a test.");

    Console.WriteLine("Resulting string: " + str);

    Console.WriteLine();

    strOp = RemoveSpaces;

    str = strOp("This is a test.");

    Console.WriteLine("Resulting string: " + str);

    Console.WriteLine();

    strOp = Reverse;

    str = strOp("This is a test.");

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   528   529   530   531   532   533   534   535   ...   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