SlideShare a Scribd company logo
2
Most read
4
Most read
11
Most read
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/

More Related Content

PDF
TypeScript
PPSX
ASP.NET Web form
PPTX
C# Events
PPTX
C# Delegates
PPTX
Typescript ppt
PPTX
LINQ in C#
PPTX
C# Tutorial
PPTX
CSharp Presentation
TypeScript
ASP.NET Web form
C# Events
C# Delegates
Typescript ppt
LINQ in C#
C# Tutorial
CSharp Presentation

What's hot (20)

PPTX
Getting started with typescript
PPT
TypeScript Presentation
PDF
javascript objects
PPTX
C# Delegates
PPTX
Operator overloading and type conversion in cpp
PDF
jQuery for beginners
PDF
Java 8 Lambda Expressions
PPTX
PDF
Callback Function
PPT
Developing an ASP.NET Web Application
PPTX
String, string builder, string buffer
PPTX
Python Lambda Function
PPTX
Delegates and events in C#
PPTX
Asp.NET Validation controls
PPTX
Typescript Fundamentals
PPT
C# Exceptions Handling
PDF
JavaScript - Chapter 8 - Objects
PPTX
Properties and indexers in C#
PDF
Asynchronous JavaScript Programming
PPTX
Explain Delegates step by step.
Getting started with typescript
TypeScript Presentation
javascript objects
C# Delegates
Operator overloading and type conversion in cpp
jQuery for beginners
Java 8 Lambda Expressions
Callback Function
Developing an ASP.NET Web Application
String, string builder, string buffer
Python Lambda Function
Delegates and events in C#
Asp.NET Validation controls
Typescript Fundamentals
C# Exceptions Handling
JavaScript - Chapter 8 - Objects
Properties and indexers in C#
Asynchronous JavaScript Programming
Explain Delegates step by step.
Ad

Viewers also liked (20)

PPTX
Windows Phone Application Development
PPTX
Debugging C# Applications
PPTX
Universal Apps for Windows Devices
PPTX
Windows Runtime Apps
PPTX
Windows Runtime Apps
PPTX
Building Universal Apps for Windows and Windows Phone
PPTX
Introduction to Universal Apps-Jaliya Udagedara
PPTX
Introduction to Universal Apps
PPTX
Windows communication foundation (part1) jaliya udagedara
PPTX
Let's Explore C# 6
PPTX
Getting Started Developing Universal Windows Platform (UWP) Apps
PPTX
Windows communication foundation (part2) jaliya udagedara
PPTX
Generics In and Out
PPTX
Clean code em C#
PPTX
C# in depth
PPS
Let Us Learn Lambda Using C# 3.0
PPTX
Universal Apps for Windows Devices
PPTX
Clean Code
PPTX
Fun with lambda expressions
PPTX
Writing clean code in C# and .NET
Windows Phone Application Development
Debugging C# Applications
Universal Apps for Windows Devices
Windows Runtime Apps
Windows Runtime Apps
Building Universal Apps for Windows and Windows Phone
Introduction to Universal Apps-Jaliya Udagedara
Introduction to Universal Apps
Windows communication foundation (part1) jaliya udagedara
Let's Explore C# 6
Getting Started Developing Universal Windows Platform (UWP) Apps
Windows communication foundation (part2) jaliya udagedara
Generics In and Out
Clean code em C#
C# in depth
Let Us Learn Lambda Using C# 3.0
Universal Apps for Windows Devices
Clean Code
Fun with lambda expressions
Writing clean code in C# and .NET
Ad

Similar to Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara (20)

PPTX
Fun with lambda expressions
PPTX
PPTX
Evolution of C# delegates
PDF
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
PPT
Csharp4 delegates lambda_and_events
DOCX
Mastering C# Lambda Expressions: A Complete Guide
PDF
Review of c_sharp2_features_part_ii
PDF
Day02 01 Advance Feature in C# DH TDT
PDF
Linq & lambda overview C#.net
PPTX
Delegetes in c#
PPTX
Lambdas and Extension using VS2010
ODP
Can't Dance The Lambda
PPTX
Delegates and events
PDF
New c sharp3_features_(linq)_part_ii
PDF
Delegate
PPTX
PPTX
30csharp
PDF
Lambda Expression For anyone that needs Java Lambda notes
PPTX
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PDF
Java 8 Lambda Expressions & Streams
Fun with lambda expressions
Evolution of C# delegates
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
Csharp4 delegates lambda_and_events
Mastering C# Lambda Expressions: A Complete Guide
Review of c_sharp2_features_part_ii
Day02 01 Advance Feature in C# DH TDT
Linq & lambda overview C#.net
Delegetes in c#
Lambdas and Extension using VS2010
Can't Dance The Lambda
Delegates and events
New c sharp3_features_(linq)_part_ii
Delegate
30csharp
Lambda Expression For anyone that needs Java Lambda notes
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
Java 8 Lambda Expressions & Streams

Recently uploaded (20)

PPTX
Transform Your Business with a Software ERP System
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
System and Network Administration Chapter 2
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
ai tools demonstartion for schools and inter college
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
medical staffing services at VALiNTRY
Transform Your Business with a Software ERP System
Odoo Companies in India – Driving Business Transformation.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Odoo POS Development Services by CandidRoot Solutions
Which alternative to Crystal Reports is best for small or large businesses.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
wealthsignaloriginal-com-DS-text-... (1).pdf
How Creative Agencies Leverage Project Management Software.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Internet Downloader Manager (IDM) Crack 6.42 Build 41
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
System and Network Administration Chapter 2
Design an Analysis of Algorithms II-SECS-1021-03
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
ai tools demonstartion for schools and inter college
Reimagine Home Health with the Power of Agentic AI​
2025 Textile ERP Trends: SAP, Odoo & Oracle
medical staffing services at VALiNTRY

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

Editor's Notes

  • #16: 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)
  • #17: 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.
  • #18: 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.
  • #19: 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.