SlideShare une entreprise Scribd logo
1  sur  9
Presented By:
Malik Muzammil Nazir 20-Arid-332
Ashan Shahid 20-Arid-317
Aneeq Ahmed 20-Arid-3
Lambda expression in C#
• C# 3.0 introduced the lambda expression.
• It is also works like an anonymous method.
• The difference that in Lambda expressions you don’t need to
specify the type of the value that you input thus making it
more flexible to use.
• It means lambda expression simplifies the anonymous
function or you can say that lambda expression is a
shorthand for an anonymous function.
ANONYMOUS FUNCTION
• We already discussed that delegates are used to reference any methods that has the
same signature as that of the delegate.
• As the name suggests, an anonymous method is a method without a name just the body.
• Anonymous methods in C# can be defined using the delegate keyword.
• It is introduced in C# 2.0.
• Anonymous method can be assigned to a variable of delegate type.
• You need not specify the return type in an anonymous method; it is inferred from the
return statement inside the method body.
• We don’t required to use access modifiers with anonymous function like public, private
etc.
• We don’t required to use return type like int, string because its return type is set as
same as delegate type.
• Anonymous function is not a static and instance member.
• Points to Remember
• Anonymous method can be defined using the delegate
keyword
• Anonymous method must be assigned to a delegate.
• Anonymous method can access outer variables or functions.
• Anonymous method can be passed as a parameter.
• Anonymous method can be used as event handlers.
ADVANTAGES OF ANONYMOUS FUNCTION IN C#
ADVANTAGES:
• Lesser typing work because we don’t required to write access
modifier, return type and name of the function.
• Anonymous functions are suggested when code volumes are less.
Anonymous Method Limitations:
• It cannot contain jump statement like goto, break or continue.
• It cannot access ref or out parameter of an outer method.
How to use:
The => is the lambda operator which
is used in all lambda expressions.
Types of Lambhda
• The Lambda expression is divided into two parts, the left side is the input and
the right is the expression.
1. Statement Lambda: Consists of the input and a set of statements to be
executed.
Syntax
• input => { statements };
2. Expression Lambda: Consists of the input and the expression.
Syntax
• input => expression;
Source Code For Lambda Expression In C#
using System;
using System.Collections.Generic;
using System.Linq
;using System.Text;
using System.Threading.Tasks;
namespace LambdaExpressionDemo{ public delegate int MyDelegate(int num1, int num2);
class Program { static void Main(string[] args) {
MyDelegate obj = (a, b) => a + b;
Console.WriteLine(obj.Invoke(10,20));
int cube = obj1.Invoke(5);
Console.WriteLine(cube);
MyDelegate obj = (a) =>
{
a += 5;
return a;
}; //
Console.WriteLine(obj(5));
Console.ReadLine();
}}}
Thank You

Contenu connexe

Similaire à Lamdha.pptx

Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Simplilearn
 
15 anonymous methods, partial types and nullable types
15   anonymous methods, partial types and nullable types15   anonymous methods, partial types and nullable types
15 anonymous methods, partial types and nullable types
Tuan Ngo
 
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Jaliya Udagedara
 
METHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptxMETHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptx
ArjunKhanal8
 

Similaire à Lamdha.pptx (20)

Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
 
15 anonymous methods, partial types and nullable types
15   anonymous methods, partial types and nullable types15   anonymous methods, partial types and nullable types
15 anonymous methods, partial types and nullable types
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream API
 
30csharp
30csharp30csharp
30csharp
 
30c
30c30c
30c
 
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
 
METHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptxMETHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptx
 
Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.
 
Java8 features
Java8 featuresJava8 features
Java8 features
 
Unit 1 psp
Unit 1 pspUnit 1 psp
Unit 1 psp
 
C#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-ExpressionC#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-Expression
 
Compiler Design
Compiler Design Compiler Design
Compiler Design
 
Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic Syntax
 
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
 
Module 2: C# 3.0 Language Enhancements (Slides)
Module 2: C# 3.0 Language Enhancements (Slides)Module 2: C# 3.0 Language Enhancements (Slides)
Module 2: C# 3.0 Language Enhancements (Slides)
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem sloving
 
Cs30 New
Cs30 NewCs30 New
Cs30 New
 
Basic syntax : Algorithm,Flow chart
Basic syntax : Algorithm,Flow chartBasic syntax : Algorithm,Flow chart
Basic syntax : Algorithm,Flow chart
 
Presentation of computer
Presentation of computerPresentation of computer
Presentation of computer
 

Dernier

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 

Dernier (20)

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

Lamdha.pptx

  • 1. Presented By: Malik Muzammil Nazir 20-Arid-332 Ashan Shahid 20-Arid-317 Aneeq Ahmed 20-Arid-3
  • 2. Lambda expression in C# • C# 3.0 introduced the lambda expression. • It is also works like an anonymous method. • The difference that in Lambda expressions you don’t need to specify the type of the value that you input thus making it more flexible to use. • It means lambda expression simplifies the anonymous function or you can say that lambda expression is a shorthand for an anonymous function.
  • 3. ANONYMOUS FUNCTION • We already discussed that delegates are used to reference any methods that has the same signature as that of the delegate. • As the name suggests, an anonymous method is a method without a name just the body. • Anonymous methods in C# can be defined using the delegate keyword. • It is introduced in C# 2.0. • Anonymous method can be assigned to a variable of delegate type. • You need not specify the return type in an anonymous method; it is inferred from the return statement inside the method body. • We don’t required to use access modifiers with anonymous function like public, private etc. • We don’t required to use return type like int, string because its return type is set as same as delegate type. • Anonymous function is not a static and instance member.
  • 4. • Points to Remember • Anonymous method can be defined using the delegate keyword • Anonymous method must be assigned to a delegate. • Anonymous method can access outer variables or functions. • Anonymous method can be passed as a parameter. • Anonymous method can be used as event handlers.
  • 5. ADVANTAGES OF ANONYMOUS FUNCTION IN C# ADVANTAGES: • Lesser typing work because we don’t required to write access modifier, return type and name of the function. • Anonymous functions are suggested when code volumes are less. Anonymous Method Limitations: • It cannot contain jump statement like goto, break or continue. • It cannot access ref or out parameter of an outer method.
  • 6. How to use: The => is the lambda operator which is used in all lambda expressions.
  • 7. Types of Lambhda • The Lambda expression is divided into two parts, the left side is the input and the right is the expression. 1. Statement Lambda: Consists of the input and a set of statements to be executed. Syntax • input => { statements }; 2. Expression Lambda: Consists of the input and the expression. Syntax • input => expression;
  • 8. Source Code For Lambda Expression In C# using System; using System.Collections.Generic; using System.Linq ;using System.Text; using System.Threading.Tasks; namespace LambdaExpressionDemo{ public delegate int MyDelegate(int num1, int num2); class Program { static void Main(string[] args) { MyDelegate obj = (a, b) => a + b; Console.WriteLine(obj.Invoke(10,20)); int cube = obj1.Invoke(5); Console.WriteLine(cube); MyDelegate obj = (a) => { a += 5; return a; }; // Console.WriteLine(obj(5)); Console.ReadLine(); }}}