SlideShare une entreprise Scribd logo
1  sur  20
Lambda Expressions in
C# From Beginner To
Expert
Jaliya Udagedara
What are we going to discuss today?
Introduction to Lambda Expressions
What are Delegates in C#
The Evolution of Delegates in C#
Demo 1 - Delegates and Named Methods, Anonymous Methods
Lambdas as Generic Delegates
Action
Func
Predicate
Lambdas as Callable Entities
Lambda Expression Execution
Lambdas as Callbacks
Demo 2 – Lambda Expressions
Introduction to Lambda Expressions
Introduced with .NET 3.5/C# 3.0
Supersede anonymous methods
Anonymous methods enable you to omit the parameter
list
Unnamed, inline functions
Used wherever delegates are required
(input parameters) => expression
Expression Lambdas
(input parameters) => expression
Statement Lambdas
(input parameters) => {statement;}
Async Lambdas
(input parameters) => async {statement;}
What are Delegates in C#
Delegates are like C++ function pointers
Makes it possible to treat methods as entities.
Assign to variables
Pass as parameters
Delegates can be chained together; for
example, multiple methods can be called on a
single event.Does delegates are exactly same as C++ function
pointers?
Does methods has to match the delegate type exactly?
What are Delegates in C# contd.
Delegates are like C++ function pointers but are
type safe.
Variance in Delegates
Covariance permits a method to have return type that is
More derived than that defined in the delegate
Contravariance permits a method that has parameter
types that are less derived than those in the delegate type.
The Evolution of Delegates in C#
C# 1.0 the only way to declare a delegate was to
use named methods.
C# 2.0 introduced anonymous methods as a way to
write unnamed inline statement blocks that can be
executed in a delegate invocation.
C# 3.0 introduced lambda expressions, which are
similar in concept to anonymous methods but more
expressive and concise.
Classic Delegate Example
btnHelloWorld.Click +=
new
EventHandler(btnHelloWorld_Click);
Anonymous Methods
btnHelloWorld.Click += delegate(System.Object
sender, System.EventArgs e) { … };
Lambda Expressions
btnHelloWorld.Click += (sender, e) => { ... };
• Anonymous
Lambdas as Generic Delegates
Generic delegates were new in .NET 2.0
Action Delegates
Func Delegates
Predicate Delegates
Action Delegate
Zero, one or more input parameters, and does not
return anything.
Takes up to 16 parameters
Array.ForEach method and List.ForEach
Perform an action on each element of the array or
list
Action<int,int,string>
Func Delegate
Zero, one or more input parameters, and returns a
value
Takes up to 16 parameters
List.First
Func<int,int,string>
Predicate Delegate
Encapsulates a method that evaluates to True or
False
Takes one parameter
List.Find
Func<int>
Lambdas as Callable Entities
Lambda expressions can be assigned to a delegate
variable
Lambda expression is executed when the delegate
is executed
Func<int, string> myFunc = x =>
{
return String.Format("{0}*{0} is {1}", x, x * x);
};
Console.WriteLine(myFunc(4));
Lambda Expression Execution
Lambda expressions are executed when they are
called, not when they are constructed
Variable value used is the value at execution time
Example: Local Variables
int y = 0;
Func<int, string> myFunc = x => (x +
y).ToString();
y = 10;
Console.WriteLine(myFunc(5));
Answer?
Lambdas as Callbacks
Callback
“Executable code that is passed as an argument to other
code”
Passes a function to a function
static void DoWork()
{
CallBack(s => Console.WriteLine(s));
}
static void CallBack(Action<string> MyAction)
{
MyAction("Completed");
}
• Lambdas as Generic Delegates
• Lambdas as Callable Entities
• Lambdas as Callbacks
Thank You!
http://www.jaliyaudagedara.blogspot.com/

Contenu connexe

Tendances (20)

Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in Java
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
C# Events
C# EventsC# Events
C# Events
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Angular 8
Angular 8 Angular 8
Angular 8
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
 
Spring jdbc
Spring jdbcSpring jdbc
Spring jdbc
 
JQuery selectors
JQuery selectors JQuery selectors
JQuery selectors
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Clean Code II - Dependency Injection
Clean Code II - Dependency InjectionClean Code II - Dependency Injection
Clean Code II - Dependency Injection
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 

En vedette

Windows Phone Application Development
Windows Phone Application DevelopmentWindows Phone Application Development
Windows Phone Application DevelopmentJaliya Udagedara
 
Universal Apps for Windows Devices
Universal Apps for Windows DevicesUniversal Apps for Windows Devices
Universal Apps for Windows DevicesJaliya Udagedara
 
Building Universal Apps for Windows and Windows Phone
Building Universal Apps for Windows and Windows PhoneBuilding Universal Apps for Windows and Windows Phone
Building Universal Apps for Windows and Windows PhoneJaliya Udagedara
 
Introduction to Universal Apps-Jaliya Udagedara
Introduction to Universal Apps-Jaliya UdagedaraIntroduction to Universal Apps-Jaliya Udagedara
Introduction to Universal Apps-Jaliya UdagedaraJaliya Udagedara
 
Introduction to Universal Apps
Introduction to Universal AppsIntroduction to Universal Apps
Introduction to Universal AppsJaliya Udagedara
 
Windows communication foundation (part1) jaliya udagedara
Windows communication foundation (part1)    jaliya udagedaraWindows communication foundation (part1)    jaliya udagedara
Windows communication foundation (part1) jaliya udagedaraJaliya Udagedara
 
Getting Started Developing Universal Windows Platform (UWP) Apps
Getting Started Developing Universal Windows Platform (UWP) AppsGetting Started Developing Universal Windows Platform (UWP) Apps
Getting Started Developing Universal Windows Platform (UWP) AppsJaliya Udagedara
 
Windows communication foundation (part2) jaliya udagedara
Windows communication foundation (part2) jaliya udagedaraWindows communication foundation (part2) jaliya udagedara
Windows communication foundation (part2) jaliya udagedaraJaliya Udagedara
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Sheik Uduman Ali
 
Universal Apps for Windows Devices
Universal Apps for Windows DevicesUniversal Apps for Windows Devices
Universal Apps for Windows DevicesJaliya Udagedara
 
Fun with lambda expressions
Fun with lambda expressionsFun with lambda expressions
Fun with lambda expressionsMike Melusky
 
Writing clean code in C# and .NET
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NETDror Helper
 

En vedette (20)

Windows Phone Application Development
Windows Phone Application DevelopmentWindows Phone Application Development
Windows Phone Application Development
 
Debugging C# Applications
Debugging C# ApplicationsDebugging C# Applications
Debugging C# Applications
 
Universal Apps for Windows Devices
Universal Apps for Windows DevicesUniversal Apps for Windows Devices
Universal Apps for Windows Devices
 
Windows Runtime Apps
Windows Runtime AppsWindows Runtime Apps
Windows Runtime Apps
 
Windows Runtime Apps
Windows Runtime AppsWindows Runtime Apps
Windows Runtime Apps
 
Building Universal Apps for Windows and Windows Phone
Building Universal Apps for Windows and Windows PhoneBuilding Universal Apps for Windows and Windows Phone
Building Universal Apps for Windows and Windows Phone
 
Introduction to Universal Apps-Jaliya Udagedara
Introduction to Universal Apps-Jaliya UdagedaraIntroduction to Universal Apps-Jaliya Udagedara
Introduction to Universal Apps-Jaliya Udagedara
 
Introduction to Universal Apps
Introduction to Universal AppsIntroduction to Universal Apps
Introduction to Universal Apps
 
Windows communication foundation (part1) jaliya udagedara
Windows communication foundation (part1)    jaliya udagedaraWindows communication foundation (part1)    jaliya udagedara
Windows communication foundation (part1) jaliya udagedara
 
Let's Explore C# 6
Let's Explore C# 6Let's Explore C# 6
Let's Explore C# 6
 
Getting Started Developing Universal Windows Platform (UWP) Apps
Getting Started Developing Universal Windows Platform (UWP) AppsGetting Started Developing Universal Windows Platform (UWP) Apps
Getting Started Developing Universal Windows Platform (UWP) Apps
 
Windows communication foundation (part2) jaliya udagedara
Windows communication foundation (part2) jaliya udagedaraWindows communication foundation (part2) jaliya udagedara
Windows communication foundation (part2) jaliya udagedara
 
Generics In and Out
Generics In and OutGenerics In and Out
Generics In and Out
 
Clean code em C#
Clean code em C#Clean code em C#
Clean code em C#
 
C# in depth
C# in depthC# in depth
C# in depth
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
Universal Apps for Windows Devices
Universal Apps for Windows DevicesUniversal Apps for Windows Devices
Universal Apps for Windows Devices
 
Clean Code
Clean CodeClean Code
Clean Code
 
Fun with lambda expressions
Fun with lambda expressionsFun with lambda expressions
Fun with lambda expressions
 
Writing clean code in C# and .NET
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NET
 

Similaire à Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...Anil Sharma
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsAbed Bukhari
 
Fun with lambda expressions
Fun with lambda expressionsFun with lambda expressions
Fun with lambda expressionsMike Melusky
 
Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.Questpond
 
Day02 01 Advance Feature in C# DH TDT
Day02 01 Advance Feature in C# DH TDTDay02 01 Advance Feature in C# DH TDT
Day02 01 Advance Feature in C# DH TDTNguyen Patrick
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Featurestechfreak
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The LambdaTogakangaroo
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8Dian Aditya
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and eventsPrem Kumar Badri
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An OverviewIndrajit Das
 
Linq and lambda
Linq and lambdaLinq and lambda
Linq and lambdaJohn Walsh
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Nettjunicornfx
 

Similaire à Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara (20)

tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
 
Cs30 New
Cs30 NewCs30 New
Cs30 New
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_events
 
Of Lambdas and LINQ
Of Lambdas and LINQOf Lambdas and LINQ
Of Lambdas and LINQ
 
Fun with lambda expressions
Fun with lambda expressionsFun with lambda expressions
Fun with lambda expressions
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.
 
JDK8 Lambda expressions demo
JDK8 Lambda expressions demoJDK8 Lambda expressions demo
JDK8 Lambda expressions demo
 
Day02 01 Advance Feature in C# DH TDT
Day02 01 Advance Feature in C# DH TDTDay02 01 Advance Feature in C# DH TDT
Day02 01 Advance Feature in C# DH TDT
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
 
Opps concept
Opps conceptOpps concept
Opps concept
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
Linq and lambda
Linq and lambdaLinq and lambda
Linq and lambda
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
 

Dernier

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 

Dernier (20)

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 

Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

  • 1. Lambda Expressions in C# From Beginner To Expert Jaliya Udagedara
  • 2. What are we going to discuss today? Introduction to Lambda Expressions What are Delegates in C# The Evolution of Delegates in C# Demo 1 - Delegates and Named Methods, Anonymous Methods Lambdas as Generic Delegates Action Func Predicate Lambdas as Callable Entities Lambda Expression Execution Lambdas as Callbacks Demo 2 – Lambda Expressions
  • 3. Introduction to Lambda Expressions Introduced with .NET 3.5/C# 3.0 Supersede anonymous methods Anonymous methods enable you to omit the parameter list Unnamed, inline functions Used wherever delegates are required (input parameters) => expression
  • 4. Expression Lambdas (input parameters) => expression Statement Lambdas (input parameters) => {statement;} Async Lambdas (input parameters) => async {statement;}
  • 5. What are Delegates in C# Delegates are like C++ function pointers Makes it possible to treat methods as entities. Assign to variables Pass as parameters Delegates can be chained together; for example, multiple methods can be called on a single event.Does delegates are exactly same as C++ function pointers? Does methods has to match the delegate type exactly?
  • 6. What are Delegates in C# contd. Delegates are like C++ function pointers but are type safe. Variance in Delegates Covariance permits a method to have return type that is More derived than that defined in the delegate Contravariance permits a method that has parameter types that are less derived than those in the delegate type.
  • 7. The Evolution of Delegates in C# C# 1.0 the only way to declare a delegate was to use named methods. C# 2.0 introduced anonymous methods as a way to write unnamed inline statement blocks that can be executed in a delegate invocation. C# 3.0 introduced lambda expressions, which are similar in concept to anonymous methods but more expressive and concise.
  • 8. Classic Delegate Example btnHelloWorld.Click += new EventHandler(btnHelloWorld_Click);
  • 9. Anonymous Methods btnHelloWorld.Click += delegate(System.Object sender, System.EventArgs e) { … }; Lambda Expressions btnHelloWorld.Click += (sender, e) => { ... };
  • 11. Lambdas as Generic Delegates Generic delegates were new in .NET 2.0 Action Delegates Func Delegates Predicate Delegates
  • 12. Action Delegate Zero, one or more input parameters, and does not return anything. Takes up to 16 parameters Array.ForEach method and List.ForEach Perform an action on each element of the array or list Action<int,int,string>
  • 13. Func Delegate Zero, one or more input parameters, and returns a value Takes up to 16 parameters List.First Func<int,int,string>
  • 14. Predicate Delegate Encapsulates a method that evaluates to True or False Takes one parameter List.Find Func<int>
  • 15. Lambdas as Callable Entities Lambda expressions can be assigned to a delegate variable Lambda expression is executed when the delegate is executed Func<int, string> myFunc = x => { return String.Format("{0}*{0} is {1}", x, x * x); }; Console.WriteLine(myFunc(4));
  • 16. Lambda Expression Execution Lambda expressions are executed when they are called, not when they are constructed Variable value used is the value at execution time
  • 17. Example: Local Variables int y = 0; Func<int, string> myFunc = x => (x + y).ToString(); y = 10; Console.WriteLine(myFunc(5)); Answer?
  • 18. Lambdas as Callbacks Callback “Executable code that is passed as an argument to other code” Passes a function to a function static void DoWork() { CallBack(s => Console.WriteLine(s)); } static void CallBack(Action<string> MyAction) { MyAction("Completed"); }
  • 19. • Lambdas as Generic Delegates • Lambdas as Callable Entities • Lambdas as Callbacks

Notes de l'éditeur

  1. Works with a variety of database servers (including Microsoft SQL Server, Oracle, and DB2)Includes a rich mapping engine that can handle real-world database schemas and works well with stored proceduresProvides integrated Visual Studio tools to visually create entity models and to auto-generate models from an existing database. New databases can be deployed from a model, which can also be hand-edited for full controlProvides a Code First experience to create entity models using code. Code First can map to an existing database or generate a database from the model.Integrates well into all the .NET application programming models including ASP.NET, Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), and WCF Data Services (formerly ADO.NET Data Services)
  2. Reduced development time: the framework provides the core data access capabilities so developers can concentrate on application logic.Developers can work in terms of a more application-centric object model, including types with inheritance, complex members, and relationships. In .NET Framework 4, the Entity Framework also supports Persistence Ignorance through Plain Old CLR Objects (POCO) entities.Applications are freed from hard-coded dependencies on a particular data engine or storage schema by supporting a conceptual model that is independent of the physical/storage model.Mappings between the object model and the storage-specific schema can change without changing the application code.Language-Integrated Query support (called LINQ to Entities) provides IntelliSense and compile-time syntax validation for writing queries against a conceptual model.
  3. Reduced development time: the framework provides the core data access capabilities so developers can concentrate on application logic.Developers can work in terms of a more application-centric object model, including types with inheritance, complex members, and relationships. In .NET Framework 4, the Entity Framework also supports Persistence Ignorance through Plain Old CLR Objects (POCO) entities.Applications are freed from hard-coded dependencies on a particular data engine or storage schema by supporting a conceptual model that is independent of the physical/storage model.Mappings between the object model and the storage-specific schema can change without changing the application code.Language-Integrated Query support (called LINQ to Entities) provides IntelliSense and compile-time syntax validation for writing queries against a conceptual model.
  4. Reduced development time: the framework provides the core data access capabilities so developers can concentrate on application logic.Developers can work in terms of a more application-centric object model, including types with inheritance, complex members, and relationships. In .NET Framework 4, the Entity Framework also supports Persistence Ignorance through Plain Old CLR Objects (POCO) entities.Applications are freed from hard-coded dependencies on a particular data engine or storage schema by supporting a conceptual model that is independent of the physical/storage model.Mappings between the object model and the storage-specific schema can change without changing the application code.Language-Integrated Query support (called LINQ to Entities) provides IntelliSense and compile-time syntax validation for writing queries against a conceptual model.