C# 0 The Complete Reference


void . Inside  Main( )



Download 4,07 Mb.
Pdf ko'rish
bet521/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   517   518   519   520   521   522   523   524   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

void

. Inside 



Main( )

, a 


CountIt

 instance called 



count

 is created, and it is passed the 

block of code that follows the 

delegate

 keyword. This block of code is the anonymous 

method that will be executed when 

count

 is called. Notice that the block of code is followed 

by a semicolon, which terminates the declaration statement. The output from the program is 

shown here:

0

1

2



3

4

5



www.freepdf-books.com


410

 

P a r t   I :  



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

Pass Arguments to an Anonymous Method

It is possible to pass one or more arguments to an anonymous method. To do so, follow the 



delegate

 keyword with a parenthesized parameter list. Then, pass the argument(s) to the 

delegate instance when it is called. For example, here is the preceding program rewritten 

so that the ending value for the count is passed:

// Demonstrate an anonymous method that takes an argument.

using System;

// Notice that CountIt now has a parameter.

delegate void CountIt(int end);

class AnonMethDemo2 {

  static void Main() {

    // Here, the ending value for the count

    // is passed to the anonymous method.

    CountIt count = delegate (int end) {

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

        Console.WriteLine(i);

    };


    count(3);

    Console.WriteLine();

    count(5);

  }


}

In this version, 



CountIt

 now takes an integer argument. Notice how the parameter list 

is specified after the 

delegate

 keyword when the anonymous method is created. The code 

inside the anonymous method has access to the parameter 

end

 in just the same way it would 

if a named method were being created. The output from this program is shown next:

0

1



2

3

0



1

2

3



4

5

Return a Value from an Anonymous Method

An anonymous method can return a value. The value is returned by use of the 

return

statement, which works the same in an anonymous method as it does in a named method. 

As you would expect, the type of the return value must be compatible with the return type 

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   517   518   519   520   521   522   523   524   ...   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