Presentation is loading. Please wait.

Presentation is loading. Please wait.

6 Delegate and Lambda Expressions

Similar presentations


Presentation on theme: "6 Delegate and Lambda Expressions"— Presentation transcript:

1 6 Delegate and Lambda Expressions
.NET and .NET Core 6 Delegate and Lambda Expressions Pan Wuming 2017

2 Topics Extension Methods Delegates
Generic to Simplify Delegate Declaration Lambda Expressions References: C# Programming Guide(MSDN) Covariance and Contravariance in Generics(MSDN)

3 Coding with Denotations
Denoting Collection of Names Denoting Behavior Group Referring to Behaviors Enumerations Interfaces Delegates

4 Extension Methods Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are defined as static methods but are called by using instance method syntax.

5

6 Delegates A delegate is a type that represents references to methods with a particular parameter list and return type.

7 Delegates Delegate types are derived from the Delegate class in the .NET Framework. Delegate types are sealed. The instantiated delegate can be passed as a parameter, or assigned to a property. The instantiated delegate can wrap an instance method or a static method

8 Multicasting A delegate can call more than one method when invoked
Delegates with more than one method in their invocation list derive from MulticastDelegate MulticastDelegate is a subclass of System.Delegate. To add an extra method to the delegate's list of methods To remove a method from the invocation list

9

10 … d1 = obj.Method1; d1 += obj.Method2; 1 2 obj.Method1 _target obj
_methodPtr MethodClass.Method1 _prev null 1 _target obj _methodPtr MethodClass.Method2 _prev _target obj _methodPtr MethodClass.Method1 _prev null obj.Method2 2

11 Call The Members Defined In System.Delegate

12 Comparing delegates

13 Is the delegate referred by handler Derived from MulticastDelegate?
A Question Is the delegate referred by handler Derived from MulticastDelegate?

14 Generic to Simplify Delegate Declaration
Generic delegates The Action and Func generic delegates Covariance and Contravariance in Generics

15 Generic Delegates A delegate can define its own type parameters.
In and out Generic Modifier

16

17 The Action Generic Delegates in .NET Framework Class Library
Description Action Encapsulates a method that has no parameters and does not return a value. Action< T> Encapsulates a method that has a single parameter and does not return a value. Action< T1, T2> Encapsulates a method that has two parameters and does not return a value. ... Action< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> Encapsulates a method that has 15 parameters and does not return a value. Action< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> Encapsulates a method that has 16 parameters and does not return a value.

18 The Func Generic Delegates in .NET Framework Class Library
Description Func< TResult> Encapsulates a method that has no parameters and returns a value of the type specified by the TResult parameter. Func< T, TResult> Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter. Func< T1, T2, TResult> Encapsulates a method that has two parameters and returns a value of the type specified by the TResult parameter. ... Func< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult> Encapsulates a method that has 16 parameters and returns a value of the type specified by the TResult parameter.

19 Covariance and Contravariance
Covariance: Enables you to use a more specific type than originally specified. Contravariance: Enables you to use a more generic (less derived) type than originally specified. Invariance: Means that you can use only the type originally specified The in keyword specifies that the type parameter is contravariant. The out keyword specifies that the type parameter is covariant.

20 Contravariance

21 Covariant Return Type and Contravariant Parameter Type
Null coalescing x ?? y Evaluates to y if x is null, to x otherwise

22 Lambda Expressions Anonymous Methods The lambda operator =>
Specifying input parameters (if any) on the left side of => Putting the expression or statement block on the other side Lambdas can refer to outer variables button1.Click += delegate(System.Object o, System.EventArgs e) { System.Windows.Forms.MessageBox.Show("Click!"); };

23

24 Summarizing the Applications of Delegates
Callback Function Event members in a Type Replace simple Interface implementation

25 Actual passing and assigning direction is contravariant !
See you next class! Actual passing and assigning direction is contravariant !


Download ppt "6 Delegate and Lambda Expressions"

Similar presentations


Ads by Google