C# 0 The Complete Reference


sum  is returned by the code block that is associated with the  count



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

sum

 is returned by the code block that is associated with the 



count

 delegate instance. Notice that the 



return

 statement is used in an anonymous method 

in just the same way that it is used in a named method. The output is shown here:

0

1



2

3

Summation of 3 is 6



0

1

2



3

4

5



Summation of 5 is 15

www.freepdf-books.com




412

 

P a r t   I :  



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

Use Outer Variables with Anonymous Methods

A local variable or parameter whose scope includes an anonymous method is called an 



outer

variable.

 An anonymous method has access to and can use these outer variables. When an 

outer variable is used by an anonymous method, that variable is said to be 

captured.

 A 


captured variable will stay in existence at least until the delegate that captured it is subject 

to garbage collection. Thus, even though a local variable will normally cease to exist when 

its block is exited, if that local variable is being used by an anonymous method, then that 

variable will stay in existence at least until the delegate referring to that method is destroyed.

The capturing of a local variable can lead to unexpected results. For example, consider 

this version of the counting program. As in the previous version, the summation of the 

count is computed. However, in this version, a 

CountIt

 object is constructed and returned 

by a static method called 

Counter( )

. This object uses the variable 



sum

, which is declared in 

the enclosing scope provided by 

Counter( )

, rather than in the anonymous method, itself. 

Thus,

sum

 is captured by the anonymous method. Inside 



Main( )

,

Counter( )

 is called to 

obtain a 



CountIt

 object. Thus, 



sum

 will not be destroyed until the program finishes.

// Demonstrate a captured variable.

using System;

// This delegate returns int and takes an int argument.

delegate int CountIt(int end);

class VarCapture {

  static CountIt Counter() {

    int sum = 0;

    // Here, a summation of the count is stored

    // in the captured variable sum.

    CountIt ctObj = delegate (int end) {

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

        Console.WriteLine(i);

        sum += i;

      }


      return sum;

    };


    return ctObj;

  }


  static void Main() {

    // Get a counter.

    CountIt count = Counter();

    int result;

    result = count(3);

    Console.WriteLine("Summation of 3 is " + result);

    Console.WriteLine();

    result = count(5);

    Console.WriteLine("Summation of 5 is " + result);

www.freepdf-books.com




Download 4,07 Mb.

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