Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

EventName(SenderObject, EventArguments);

Section 89.5: Anonymous Event Handler Declaration

Event declaration:

public event EventHandler<EventArgsType> EventName;

Event handler declaration using lambda operator => and subscribing to the event:

EventName += (obj, eventArgs) => { /* Handler logic */ };

Event handler declaration using delegate anonymous method syntax:

EventName += delegate(object obj, EventArgsType eventArgs) { /* Handler Logic */ };

Declaration & subscription of an event handler that does not use the event's parameter, and so can use the above syntax without needing to specify parameters:

EventName += delegate { /* Handler Logic */ }

Invoking the event:

EventName?.Invoke(SenderObject, EventArguments);

Section 89.6: Non-Standard Event Declaration

Events can be of any delegate type, not just EventHandler and EventHandler<T>. For example:

//Declaring an event

public event Action<Param1Type, Param2Type, ...> EventName;

This is used similarly to standard EventHandler events:

//Adding a named event handler

public void HandlerName(Param1Type parameter1, Param2Type parameter2, ...) {

/* Handler logic */

}

EventName += HandlerName;

//Adding an anonymous event handler

EventName += (parameter1, parameter2, ...) => { /* Handler Logic */ };

//Invoking the event

EventName(parameter1, parameter2, ...);

It is possible to declare multiple events of the same type in a single statement, similar to with fields and local variables (though this may often be a bad idea):

public event EventHandler Event1, Event2, Event3;

This declares three separate events (Event1, Event2, and Event3) all of type EventHandler.

Note: Although some compilers may accept this syntax in interfaces as well as classes, the C# specification (v5.0 §13.2.3) provides grammar for interfaces that does not allow it, so using this in interfaces may be unreliable with di erent

GoalKicker.com – C# Notes for Professionals

498

compilers.

Section 89.7: Creating custom EventArgs containing additional data

Custom events usually need custom event arguments containing information about the event. For example MouseEventArgs which is used by mouse events like MouseDown or MouseUp events, contains information about Location or Buttons which used to generate the event.

When creating new events, to create a custom event arg:

Create a class deriving from EventArgs and define properties for necessary data.

As a convention, the name of the class should ends with EventArgs.

Example

In the below example, we create a PriceChangingEventArgs event for Price property of a class. The event data class contains a CurrentPrice and a NewPrice. The event raises when you assign a new value to Price property and lets the consumer know the value is changing and let them to know about current price and new price:

PriceChangingEventArgs

public class PriceChangingEventArgs : EventArgs

{

public PriceChangingEventArgs(int currentPrice, int newPrice)

{

this.CurrentPrice = currentPrice; this.NewPrice = newPrice;

}

public int CurrentPrice { get; private set; } public int NewPrice { get; private set; }

}

Product

public class Product

{

public event EventHandler<PriceChangingEventArgs> PriceChanging;

int price; public int Price

{

get { return price; } set

{

var e = new PriceChangingEventArgs(price, value); OnPriceChanging(e);

price = value;

}

}

protected void OnPriceChanging(PriceChangingEventArgs e)

{

var handler = PriceChanging; if (handler != null)

handler(this, e);

}

GoalKicker.com – C# Notes for Professionals

499

}

You can enhance the example by allowing the consumer to change the new value and then the value will be used for property. To do so it's enough to apply these changes in classes.

Change the definition of NewPrice to be settable:

public int NewPrice { get; set; }

Change the definition of Price to use e.NewPrice as value of property, after calling OnPriceChanging :

int price; public int Price

{

get { return price; } set

{

var e = new PriceChangingEventArgs(price, value); OnPriceChanging(e);

price = e.NewPrice;

}

}

GoalKicker.com – C# Notes for Professionals

500