Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 7. Events, Delegates, Generics.

Similar presentations


Presentation on theme: "Lesson 7. Events, Delegates, Generics."— Presentation transcript:

1 Lesson 7. Events, Delegates, Generics.
Programming in C# Lesson 7. Events, Delegates, Generics.

2 Delegates

3 What is a delegate? A delegate is a type safe pointer to a method (or a chain of methods) All delegates are classes that inherited from MulticastDelegate class Delegates are useful in callback scenarios Delegates are used in APM (Asynchronous Programming Model) Delegates are the base of events

4 Declaring a delegate delegate keyword Name
Return value type Parameters (number and type)

5 Using a delegate

6 Using a delegate as a callback method

7 Using a delegate as a callback method

8 Delegates chaining

9 Delegates chaining

10 Using delegates in APM

11 Anonymous Methods and Lambda Expressions

12 Events Events provide a way for an object to be notified about something that has happened in another object

13 Designing a Type That Exposes an Event
Define a type that will hold any additional information that should be sent to subscribers Define the event member (based on existing or custom delegate) Define a method responsible for raising the event Call the method responsible for raising the event from the appropriate place

14 Defining a type that will hold additional information

15 Defining the event member

16 Defining a method responsible for raising the event

17 Calling the method responsible for raising the event

18 Subscribing/Unsubscribing to events

19 Method GetInvocationList
Method GetInvocationList allows us to iterate manually through the list of subscribers and handle them correspondingly:

20 EventHandler and EventHandler<TEventArgs> delegates
In order to avoid the necessity to create custom delegates each time we need to expose an event, these standard delegates should be used:

21 Generics

22 What are generics? Generics provide another form of code reuse: algorithm reuse Generics allow us to encapsulate an algorithm within a class, interface or method without specifying the data type(s) that class or method works on Generics provide type safety Generics provide better performance (especially for collection data structures) as they prevent the necessity of boxing/unboxing and type casting There are generic classes, interfaces, methods and delegates

23 A generic class sample

24 Operator default Operator default specifies the default value of the type parameter. This will be null for reference types and zero for value types

25 Constraints on Type Parameters
where T: struct – The type argument must be a value type where T: class – The type argument must be a reference type where T: new() – The type argument must have a public parameterless constructor where T: <base class name> – The type argument must be or derive from the specified base class where T: <interface name> – The type argument must be or implement the specified interface

26 Generic Class with Constraints Sample

27 Generic Method with Constraints Sample


Download ppt "Lesson 7. Events, Delegates, Generics."

Similar presentations


Ads by Google