C# 0 The Complete Reference


Use Anonymous Methods and Lambda Expressions with Events



Download 4,07 Mb.
Pdf ko'rish
bet544/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   540   541   542   543   544   545   546   547   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Use Anonymous Methods and Lambda Expressions with Events

Anonymous methods and lambda expressions are especially useful when working with 

events because often the event handler is not called by any code other than the event 

handling mechanism. As a result, there is usually no reason to create a standalone method. 

Thus, the use of lambda expressions or anonymous methods can significantly streamline 

event handling code.

Since lambda expressions are now the preferred approach, we will start there. Here is an 

example that uses a lambda expression as an event handler:

// Use a lambda expression as an event handler.

using System;

// Declare a delegate type for an event.

delegate void MyEventHandler(int n);

// Declare a class that contains an event.

class MyEvent {

  public event MyEventHandler SomeEvent;

  // This is called to fire the event.

  public void OnSomeEvent(int n) {

    if(SomeEvent != null)

      SomeEvent(n);

  }


}

class LambdaEventDemo {

  static void Main() {

    MyEvent evt = new MyEvent();

    // Use a lambda expression as an event handler.

    evt.SomeEvent += (n) =>

      Console.WriteLine("Event received. Value is " + n);

    // Fire the event twice.

    evt.OnSomeEvent(1);

    evt.OnSomeEvent(2);

  }

}

www.freepdf-books.com




430

 

P a r t   I :  



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

The output is shown here:

Event received. Value is 1

Event received. Value is 2

In the program, pay special attention to the way the lambda expression is used as an 

event handler, as shown here:

evt.SomeEvent += (n) =>

  Console.WriteLine("Event received. Value is " + n);

The syntax for using a lambda expression event handler is the same as that for using a 

lambda expression with any other type of delegate.

Although lambda expressions are now the preferred way to construct an anonymous 

function, you can still use an anonymous method as an event handler if you so choose. For 

example, here is the event handler from the previous example rewritten to use an 

anonymous method:

// Use an anonymous method as an event handler.

evt.SomeEvent += delegate(int n)  {

  Console.WriteLine("Event received. Value is" + n);

};

As you can see, the syntax for using an anonymous event handler is the same as that for any 



anonymous method.


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   540   541   542   543   544   545   546   547   ...   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