C# 0 The Complete Reference


PART I C h a p t e r   1 5



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


PART I

C h a p t e r   1 5 :  

D e l e g a t e s ,   E v e n t s ,   a n d   L a m b d a   E x p r e s s i o n s  

419


PART IPART I

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

  }

}

The output, which is the same as the original version, is shown here:



Replacing spaces with hyphens.

Resulting string: This-is-a-test.

Removing spaces.

Resulting string: Thisisatest.

Reversing string.

Resulting string: .tset a si sihT



Events

Another important C# feature is built upon the foundation of delegates: the 



event.

 An event 

is, essentially, an automatic notification that some action has occurred. Events work like this: 

An object that has an interest in an event registers an event handler for that event. When the 

event occurs, all registered handlers are called. Event handlers are represented by delegates.

Events are members of a class and are declared using the 



event

 keyword. Its most 

commonly used form is shown here:

event


event-delegate event-name

;

Here, 



event-delegate

 is the name of the delegate used to support the event, and 



event-name

 is 


the name of the specific event object being declared.

Let’s begin with a very simple example:

// A very simple event demonstration.

using System;

// Declare a delegate type for an event.

delegate void MyEventHandler();

// Declare a class that contains an event.

class MyEvent {

  public event MyEventHandler SomeEvent;

  // This is called to fire the event.

  public void OnSomeEvent() {

    if(SomeEvent != null)

      SomeEvent();

  }


}

class EventDemo {

  // An event handler.

  static void Handler() {

    Console.WriteLine("Event occurred");

  }


www.freepdf-books.com


420

 

P a r t   I :  



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

  static void Main() {

    MyEvent evt = new MyEvent();

    // Add Handler() to the event list.

    evt.SomeEvent += Handler;

    // Fire the event.

    evt.OnSomeEvent();

  }


}

This program displays the following output:

Event occurred

Although simple, this program contains all the elements essential to proper event 

handling. Let’s look at it carefully. The program begins by declaring a delegate type for 

the event handler, as shown here:

delegate void MyEventHandler();

All events are activated through a delegate. Thus, the event delegate type defines the return 

type and signature for the event. In this case, there are no parameters, but event parameters 

are allowed.

Next, an event class, called 


Download 4,07 Mb.

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