Delegates/ Anders Børjesson

Slides:



Advertisements
Similar presentations
Lambda Expressions Matt Van Horn Software Engineer Iverson Gaming Systems, Inc.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
.NET 3.5 – Mysteries. NetFx Evolution NetFx 1.0 C# 1.0, VB 7.0, VS.NET NetFx 1.1 C# 1.1, VB 7.1, VS 2003 NetFx 2.0 C# 2.0, VB 8.0, VS 2005 NetFx 3.0 C#
Threads in C# Threads in C#.
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++ data types. Structs vs. Classes C++ Classes.
EXAMPLE 3 Combining Like Terms a. 3x + 4x = (3 + 4)x = 7x b.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
C# vs. C++ What's Different & What's New. An example C# public sometype myfn { get; set; } C++ public: sometype myfn { sometype get (); void set (sometype.
Multiple Choice Solutions True/False a c b e d   T F.
Events in C# Events in C#.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Events in C# MHA Delegates vs. Events Delegates can be used as events Example CountDownTimerEvent -> CountDownDelegate But have certain problems.
References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Lambda Expressions Version 1.0
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Functional Programming IN NON-FUNCTIONAL LANGUAGES.
Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
LINQ Language Integrated Query LINQ1. LINQ: Why and what? Problem Many data sources: Relational databases, XML, in-memory data structures, objects, etc.
Classes - Intermediate
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
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 –
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
Part 1: Overview of LINQ Intro to LINQ Presenter: PhuongNQK.
Minimising memory churn
C# for C++ Programmers 1.
Creating Your Own Classes
Chapter 15 Event-Driven Programming and Animations
Lambda Expressions Version 1.1
Advanced .NET Programming I 3nd Lecture
/* LIFE RUNS ON CODE*/ Konstantinos Pantos Microsoft MVP ASP.NET
Lambda Expressions By Val Feldsher.
Upgrading Your C# Programming Skills to Be a More Effective Developer
Critical sections, locking, monitors, etc.
CompSci 230 S Programming Techniques
Chapter Eleven Handling Events.
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Generics, Lambdas, Reflections
Creating Your OwnClasses
.NET and .NET Core: Languages, Cloud, Mobile and AI
Functional Programming with Java
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
6 Delegate and Lambda Expressions
Chapter 15 Event-Driven Programming and Animations
Conditional Statements
COMBINING LIKE TERMS & DISTRIBUTIVE PROPERTY
Lesson 7. Events, Delegates, Generics.
Implementing Classes Chapter 3.
Generics in C# / Anders Børjesson
COMBINING LIKE TERMS.
Generics, Lambdas, Reflections
Class.
5. 3 Coding with Denotations
Groovy.
C++ data types.
Advanced .NET Programming I 4th Lecture
Advanced .NET Programming I 6th Lecture
Chengyu Sun California State University, Los Angeles
Events, Delegates, and Lambdas
Presentation transcript:

Delegates/ Anders Børjesson Delegates in C# Delegates/ Anders Børjesson

Delegates/ Anders Børjesson Functions Pure mathematical function Input –> function -> output Same input = same output No side effects Methods in programming can have side effects Example: Set I properties changes the state of an object Bard De Smet: C# 5.0 Unleashed, Sams 2013: Language evolution in a broader perspective, page 791 Delegates/ Anders Børjesson

Delegates/ Anders Børjesson What are delegates? Delegates enable you to represent a function as an object Delegates are types in C# Like classes, structs, enums, etc. Syntax (for declaring delegate types) delegate returnType Name(parameters) delegate int BinaryOperation(int a, int b) Example: DelegatesTrying Syntax (for instantiating delegate types) int Plus(int a, int b) { return a + b; } BinaryOperation operation = Plus operation += Minus; Syntax (calling) int result = operation(4,5); Delegates/ Anders Børjesson

Some popular delegates types from the C# API Delegate void ThreadStart() No parameters, returns void Used in class Thread Constructor Thread(ThreadStart start) Delegate void Action<T>(T obj) This delegate type is generic On parameter of type T, returns void Used in List.ForEach(Action action) Delegate void Action<T1, T2>(T1 obj1, T2 obj2) Two parameters, returns void Delegate TResult Func<out TResult>() This delegate type is generic! No paramters, returns TResult. Out: TResult is an output parameter (there is more to out) Delegate TResult Func<in T1, in T2, out TResult>(T1 obj1, T2 obj2) Delegate bool Predicate<in T>(T obj) Delegates/ Anders Børjesson

Delegates/ Anders Børjesson Lambda expressions New feature in C# 3.0 Simplified way to write anonymous functions Including references to the closure Lambda expressions makes it simpler to instantiate delegates Example: i => i % 2 == 0 Reads: i goes to … Left hand side: parameter Right hand side: an expression Example: DelegatesTrying -> LambdaExample Example: ExtensibleCalculator Delegates/ Anders Børjesson

Closures: Captured outer variables Lambda expressions can refer to outer variables Called the closure Foreach (int i in Enumerable.Range(0, 10) { new Thread(() => Console.WriteLine(i)).Start(); } Might show each number more than once The closure object of the delegate refers to the variable ‘i’ – and the value keeps changing. Solution: Make a local variable inside the loop: int i2 = i; Visual Studio: Right click the project (not the solution) and chose Properties Example: DelegatesTrying Delegates/ Anders Børjesson

Combining delegates: MultiCastDelegate All delegates are MultiCastDelegates Can refer to a number of delegate instances Example: DelegatesTrying -> BinaryOperations Example: CountDownTimer Delegates/ Anders Børjesson

Delegate vs. single-method interface A delegate is comparable to a single-method interface Delegate requires less code. Interface must be implemented by a class Interface is more flexible Can be extended Delegates/ Anders Børjesson

References and further readings MSDN Delegates (C# Programming Guide) http://msdn.microsoft.com/en-us/library/ms173171.aspx Bart De Smet: C# 5.0 Unleashed, Sams 2013 Chapter 17 Delegates, page 789-842 Oliver Sturm: Functional Programming in C#, Wrox/Wiley 2011 Chapter 3 Functions, Delegates, and Lambda Expressions, page 17-30 Nagel et al. Professional C# 5.0 and .NET 4.5.1, Wrox 2014 Chapter 8: Delegates, Lambdas, and Events, page 183-208 Delegates/ Anders Børjesson