6 Delegate and Lambda Expressions

Slides:



Advertisements
Similar presentations
Introduction to Web Application
Advertisements

Arrays.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Delegates and Events Tom Roeder CS fa. Motivation – Function Pointers Treat functions as first-class objects eg. Scheme: (map myfunc somelist)
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Ruby (on Rails) CSE 190M, Spring 2009 Week 4. Constructors Writing a new class is simple! Example: class Point end But we may want to initialize state.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Eric Vogel Software Developer A.J. Boggs & Company.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Delegates Programming in C# Delegates CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Delegates and Events Callback Functionality and Event-Driven Programming Svetlin Nakov Technical Trainer Software University
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Martin Kruliš by Martin Kruliš (v1.0)1.
Object Oriented Software Development
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
1.NETDelegates & eventsNOEA / PQC Delegates & events Observer pattern Delegates –Semantics –Cil – kode –Anvendelse Events.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
V 1.0 OE-NIK HP 1 Advanced Programming Delegates, Events.
Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Building Web Applications with Microsoft ASP
Sections Inheritance and Abstract Classes
Lambda Expressions Version 1.1
Advanced .NET Programming I 3nd Lecture
Polymorphism, Interfaces & Operator Overloading
Delegates and Events Svetlin Nakov Telerik Corporation
© 2016, Mike Murach & Associates, Inc.
2.7 Inheritance Types of inheritance
Lambda Expressions By Val Feldsher.
Module 5: Common Type System
Delegates and Events 14: Delegates and Events
© 2016, Mike Murach & Associates, Inc.
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Chapter 5: Programming with C#
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Delegates/ Anders Børjesson
.NET and .NET Core: Languages, Cloud, Mobile and AI
Lecture 23 Polymorphism Richard Gesick.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
C# Event Processing Model
AVG 24th 2015 ADVANCED c# - part 1.
Delegates & Events 1.
Lesson 7. Events, Delegates, Generics.
Fundaments of Game Design
CIS 199 Final Review.
5. 3 Coding with Denotations
IT College 2016, Andres käver
Abstract Data Types Abstraction is to distill a system to its most fundamental parts. Applying the abstraction paradigm to the design of data structures.
ITE “A” GROUP 2 ENCAPSULATION.
Chengyu Sun California State University, Los Angeles
Events, Delegates, and Lambdas
Generics, Lambdas and Reflection
Presentation transcript:

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

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

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

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.

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

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

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

… 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

Call The Members Defined In System.Delegate

Comparing delegates

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

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

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

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.

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.

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.

Contravariance

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

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!"); };

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

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