Download presentation
Presentation is loading. Please wait.
1
Tech·Ed North America 2009 5/18/2018 2:05 PM
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
2
Getting the Most from Lambda Expressions
Tech·Ed North America 2009 5/18/2018 2:05 PM Getting the Most from Lambda Expressions Deborah Kurata President InStep Technologies, Inc Session Code: DTL315 © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
3
Deborah Kurata is... Consultant and President of: InStep Technologies, Inc. Author of: Doing Objects in VB 2005 Best Kept Secrets in .NET Software designer/developer INETA Speaker Microsoft MVP
4
InStep Technologies is...
Consulting Mentoring services to get you started and keep you going with .NET Strategic software consulting and training services Application architecture and design Custom software development of Windows and Web-based applications Web site:
5
You Are... Using Generic Lists Using Lambda Expressions
List(Of Customer) List<Customer> Using Lambda Expressions Just getting started/new to them Use them once in a while Use them everyday
6
This Presentation is … Fun with Lambda Expressions
What are Lambda Expressions? Lambdas as Generic Delegates Lambdas as Callable Entities Lambdas as Callbacks
7
Generic Lists Class Customer Class Customers Manages a single customer
Customer properties Customer methods Class Customers Manages the list of customers List(Of Customer) List<Customer>
8
Fun With Lambda Expressions
5/18/2018 2:05 PM demo Fun With Lambda Expressions © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
9
Lambda Calculus “Formal system designed to investigate function definition, function application, and recursion.” Wikipedia
10
What are Lambda Expressions?
New in Visual Basic 9 and C# 3.0 (VS 2008) Unnamed, inline functions Single statement Multiple statement (C# only; coming in VB 10) Used wherever delegates are required
11
What is a Delegate? An object that holds a reference to a method with a particular parameter list and return type Like object-oriented, type-safe function pointer Make it possible to treat methods as entities Assign to variables Pass as parameters
12
Classic Delegate Example: Events
// Passes the method to the new delegate HelloButton.Click += new EventHandler(HelloButton_Click); ' AddressOf assigns the address of the ' method to the delegate AddHandler HelloButton.Click, _ AddressOf HelloButton_Click
13
Using Lambda Expressions
// C# HelloButton.Click += (s, ev) => MessageBox.Show("Hello World"); ' VB: AddHandler HelloButton.Click, _ Function(s, ev) _ MessageBox.Show("Hello World")
14
Defining Delegates Named method Anonymous method (C# 2.0)
Lambda expression (C# 3.0 and VB 9)
15
Lambda Expression Syntax - VB
' General syntax: Dim foundCustomer as Customer = _ allCust.First(Function(c as Customer) _ c.CustomerId = 4) ' With inferred typing: Dim foundCustomer = _ allCust.First(Function(c) _
16
Lambda Expression Syntax – C#
' General syntax: Customer foundCustomer = allCust.First((Customer c) => c.CustomerId == 4); ' With inferred typing: var foundCustomer = allCust.First(c =>
17
Lambda Multiline Syntax – C#
var foundCustomer = allCust.First(c => { Debug.WriteLine(c.FullName); if (c.CustomerId == 4) return true; else return false; });
18
Features that Use Delegates
Event handlers Enumerable class methods Extension methods of IEnumerable(T) Examples: Aggregate, All, Any, FirstOrDefault LINQ methods Used by LINQ Examples: OrderBy, Where, GroupBy
19
Lambdas as Generic Delegates
Generic delegates were new in .NET 2.0 Predicate Delegates Action Delegates Func Delegates
20
Predicate Delegate Predicate: “An operator or function which returns a Boolean value” - Wikipedia Encapsulates a method that evaluates to True or False Takes one parameter
21
Example: Predicate Delegate
' VB: Dim foundCustomer = _ Array.Find(custArray, Function(c) _ c.LastName.StartsWith("K")) // C#: var foundCustomer = Array.Find(custArray, c => c.LastName.StartsWith("K"));
22
demo Predicate Delegates 5/18/2018 2:05 PM
© 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
23
Action Delegate Encapsulates a method that does not return a value
Sub in VB void in C# Takes up to four parameters
24
Example: Action Delegate
' VB: Can only use named methods allCust.ForEach(AddressOf WriteToDebug) // C#: allCust.ForEach(c => Debug.WriteLine(c.FullName));
25
demo Action Delegates 5/18/2018 2:05 PM
© 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
26
Func Delegate Encapsulates a method that returns a value
Takes up to 4 parameters and a return value Last parameter is the return value Func<int, int, string> Func(Of Integer, Integer, String)
27
Example: Func Delegate
' VB: Dim total = allCust.Sum(Function(c) _ c.SalesTotal) // C#: var total = allCust.Sum(c => c.SalesTotal);
28
demo Func Delegates 5/18/2018 2:05 PM
© 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
29
Lambdas as Callable Entities
Lambda expressions can be assigned to a delegate variable Lambda expression is executed when the delegate is executed
30
Example: Assignment ' VB: Infers the type Dim f = _
Function(x as Integer) (x + x).ToString() Debug.WriteLine(f(5)) Debug.WriteLine(f(10)) // C#: Func<int,string> f = x => (x + x).ToString(); Debug.WriteLine(f(5)); Debug.WriteLine(f(10));
31
Lambda Expression Execution
Lambda expressions are executed when they are called, not when they are constructed Local variables used in a lambda expression are “captured” or “lifted” Variable value used is the value at execution time Variable lifetime extends to the lifetime of the delegate
32
Example: Local Variables
int y = 0; Func<int,string> f = x => (x + y).ToString(); y = 10; Debug.WriteLine(f(5)); ' VB: Dim y As Integer = 0 Dim f As Func(Of Integer, String) = _ Function(x) (x + y).ToString() y = 10 Debug.WriteLine(f(5))
33
demo Callable Entities 5/18/2018 2:05 PM
© 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
34
Lambdas as Callbacks Callback Passes a function to a function
“Executable code that is passed as an argument to other code” Passes a function to a function A delegate does not know or care about the class of the method it is referencing No need to reference the class that defined the method
35
5/18/2018 2:05 PM demo Callbacks © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
36
Getting the Most from Lambdas
Use Lambda expressions for Writing inline functions Calling LINQ methods Calling extension methods that take a delegate Creating callable functions Performing callbacks
37
Resources www.microsoft.com/teched www.microsoft.com/learning
Sessions On-Demand & Community Microsoft Certification & Training Resources Resources for IT Professionals Resources for Developers Microsoft Certification and Training Resources
38
Related Content DTL336 Future Directions for Visual Basic Presenter(s): Jonathan Aneja, Anders Hejlsberg DTL402 How LINQ Works: A Deep Dive into the Microsoft Visual Basic and C# Implementations Presenter: Jonathan Aneja
39
Track Resources Visit the DPR TLC for a chance to win a copy of Visual Studio Team Suite. Daily drawing occurs every day in the TLC at 4:15pm. Stop by for a raffle ticket Please visit us in the TLC blue area
40
Complete an evaluation on CommNet and enter to win!
41
5/18/2018 2:05 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.