SlideShare a Scribd company logo
1 of 4
Download to read offline
C# Lambda Expression
 Lambda expressions use special syntax. They allow
functions to be used as data such as variables or
fields. The lambda expression syntax uses the =>
operator. This separates the parameters and
statement body of the anonymous function.
Example :~
 This program uses lambda expressions and other
anonymous functions. The source text uses the =>
operator. This is a syntax for separating the
parameters to a method from the statements in the
method's body.
 Tip: Lambda expressions use the token => in an
expression context. In this context, the token is not
a comparison operator.
Token :~
 The => operator can be read as "goes to" and it is
always used when declaring a lambda expression. A
lambda expression allows you to use a function with
executable statements as a parameter, variable or
field.
Methods :~
using System;
class Program
{
static void Main()
{
//
// Use implicitly typed lambda expression.
// ... Assign it to a Func instance.
//
Func<int, int> func1 = x => x + 1;
//
// Use lambda expression with statement body.
//
Func<int, int> func2 = x => {return x + 1; };
//
// Use formal parameters with expression body.
//
Func<int, int> func3 = (int x) => x + 1;
//
// Use parameters with a statement body.
//
Func<int, int> func4 = (int x) => { return x + 1; };
//
// Use multiple parameters.
//
Func<int, int, int> func5 = (x, y) => x * y;
//
// Use no parameters in a lambda expression.
//
Action func6 = () => Console.WriteLine();
//
// Use delegate method expression.
//
Func<int, int> func7 = delegate(int x) { return x + 1;
};
//
// Use delegate expression with no parameter list.
//
Func<int> func8 = delegate { return 1 + 1; };
// Invoke each of the lambda expressions and delegates
we created.
// ... The methods above are executed.
Console.WriteLine(func1.Invoke(1));
Console.WriteLine(func2.Invoke(1));
Console.WriteLine(func3.Invoke(1));
Console.WriteLine(func4.Invoke(1));
Console.WriteLine(func5.Invoke(2, 2));
func6.Invoke();
Console.WriteLine(func7.Invoke(1));
Console.WriteLine(func8.Invoke());
}
}
Output
2
2
2
2
4
2
2
In the example, we see the => syntax. This can be read as "goes
to." It separates the arguments from the method body of a lambda
expression. It is not a comparison operator. The => syntax
separates the left from the right.
Left side: This is an empty parameter list, a formal parameter list,
or an implicit parameter list from the body.
Right side: This can be a statement list inside curly brackets with
a return statement, or an expression.
C# LINQ
 LINQ. Imperative code describes how to complete an
algorithm.
It proceeds step by step, emphasizing process, not
result. Declarative code (like LINQ) describes the
end result
 This technology, Language Integrated Query,
introduces extension methods. These work on Lists
and arrays. We even use them on collections not yet
in memory.
 Example:We use the Average extension method to
average all the elements in an int array. A double
value is returned.
 Tip:The Average method is implemented as an
extension method within the .NET Framework.
Extension methods have special syntax.
Program that uses LINQ extension: C#
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] array = { 1, 3, 5, 7 };
Console.WriteLine(array.Average());
}
}
Output
4

More Related Content

What's hot

C language presentation
C language presentationC language presentation
C language presentation
bainspreet
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
kapil078
 

What's hot (20)

Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C language presentation
C language presentationC language presentation
C language presentation
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLABAnonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLAB
 
User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Cpp functions
Cpp functionsCpp functions
Cpp functions
 
Learn Java Part 2
Learn Java Part 2Learn Java Part 2
Learn Java Part 2
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 
Storage class
Storage classStorage class
Storage class
 
C functions list
C functions listC functions list
C functions list
 
Function C++
Function C++ Function C++
Function C++
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
 
Java 8
Java 8Java 8
Java 8
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
Storage classes
Storage classesStorage classes
Storage classes
 
PLSQL CURSOR
PLSQL CURSORPLSQL CURSOR
PLSQL CURSOR
 

Viewers also liked (18)

Lecture1 oopj
Lecture1 oopjLecture1 oopj
Lecture1 oopj
 
Lecture6 oopj
Lecture6 oopjLecture6 oopj
Lecture6 oopj
 
MTA animations graphics_accessing data
MTA animations graphics_accessing dataMTA animations graphics_accessing data
MTA animations graphics_accessing data
 
Business and new economic enviornment
Business and new economic enviornment Business and new economic enviornment
Business and new economic enviornment
 
Javascript regular expression
Javascript regular expressionJavascript regular expression
Javascript regular expression
 
Css box model
Css box modelCss box model
Css box model
 
Software engg. pressman_ch-21
Software engg. pressman_ch-21Software engg. pressman_ch-21
Software engg. pressman_ch-21
 
MTA css layouts
MTA css layoutsMTA css layouts
MTA css layouts
 
Chapter 8 switching -computer_network
Chapter 8   switching -computer_networkChapter 8   switching -computer_network
Chapter 8 switching -computer_network
 
Lecture3 oopj
Lecture3 oopjLecture3 oopj
Lecture3 oopj
 
Chapter 5 analog transmission computer_network
Chapter 5 analog transmission  computer_networkChapter 5 analog transmission  computer_network
Chapter 5 analog transmission computer_network
 
Lecture9 oopj
Lecture9 oopjLecture9 oopj
Lecture9 oopj
 
MTA managing the graphical interface by using css
MTA managing the graphical interface by using cssMTA managing the graphical interface by using css
MTA managing the graphical interface by using css
 
Website design principles
Website design principlesWebsite design principles
Website design principles
 
Lecture7 oopj
Lecture7 oopjLecture7 oopj
Lecture7 oopj
 
MTA html5 text_graphics_media
MTA html5 text_graphics_mediaMTA html5 text_graphics_media
MTA html5 text_graphics_media
 
Software engg. pressman_ch-8
Software engg. pressman_ch-8Software engg. pressman_ch-8
Software engg. pressman_ch-8
 
Ajax
Ajax Ajax
Ajax
 

Similar to Linq & lambda overview C#.net

Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
mussawir20
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 

Similar to Linq & lambda overview C#.net (20)

Functions
FunctionsFunctions
Functions
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Python_UNIT-I.pptx
Python_UNIT-I.pptxPython_UNIT-I.pptx
Python_UNIT-I.pptx
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
 
Function in C++
Function in C++Function in C++
Function in C++
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
C#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-ExpressionC#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-Expression
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional Programming
 
Javascript
JavascriptJavascript
Javascript
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Python-review1.ppt
Python-review1.pptPython-review1.ppt
Python-review1.ppt
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 

Recently uploaded

Recently uploaded (20)

Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 

Linq & lambda overview C#.net

  • 1. C# Lambda Expression  Lambda expressions use special syntax. They allow functions to be used as data such as variables or fields. The lambda expression syntax uses the => operator. This separates the parameters and statement body of the anonymous function. Example :~  This program uses lambda expressions and other anonymous functions. The source text uses the => operator. This is a syntax for separating the parameters to a method from the statements in the method's body.  Tip: Lambda expressions use the token => in an expression context. In this context, the token is not a comparison operator. Token :~  The => operator can be read as "goes to" and it is always used when declaring a lambda expression. A lambda expression allows you to use a function with executable statements as a parameter, variable or field. Methods :~ using System; class Program { static void Main() { // // Use implicitly typed lambda expression. // ... Assign it to a Func instance.
  • 2. // Func<int, int> func1 = x => x + 1; // // Use lambda expression with statement body. // Func<int, int> func2 = x => {return x + 1; }; // // Use formal parameters with expression body. // Func<int, int> func3 = (int x) => x + 1; // // Use parameters with a statement body. // Func<int, int> func4 = (int x) => { return x + 1; }; // // Use multiple parameters. // Func<int, int, int> func5 = (x, y) => x * y; // // Use no parameters in a lambda expression. // Action func6 = () => Console.WriteLine(); // // Use delegate method expression. // Func<int, int> func7 = delegate(int x) { return x + 1; }; // // Use delegate expression with no parameter list. // Func<int> func8 = delegate { return 1 + 1; };
  • 3. // Invoke each of the lambda expressions and delegates we created. // ... The methods above are executed. Console.WriteLine(func1.Invoke(1)); Console.WriteLine(func2.Invoke(1)); Console.WriteLine(func3.Invoke(1)); Console.WriteLine(func4.Invoke(1)); Console.WriteLine(func5.Invoke(2, 2)); func6.Invoke(); Console.WriteLine(func7.Invoke(1)); Console.WriteLine(func8.Invoke()); } } Output 2 2 2 2 4 2 2 In the example, we see the => syntax. This can be read as "goes to." It separates the arguments from the method body of a lambda expression. It is not a comparison operator. The => syntax separates the left from the right. Left side: This is an empty parameter list, a formal parameter list, or an implicit parameter list from the body. Right side: This can be a statement list inside curly brackets with a return statement, or an expression.
  • 4. C# LINQ  LINQ. Imperative code describes how to complete an algorithm. It proceeds step by step, emphasizing process, not result. Declarative code (like LINQ) describes the end result  This technology, Language Integrated Query, introduces extension methods. These work on Lists and arrays. We even use them on collections not yet in memory.  Example:We use the Average extension method to average all the elements in an int array. A double value is returned.  Tip:The Average method is implemented as an extension method within the .NET Framework. Extension methods have special syntax. Program that uses LINQ extension: C# using System; using System.Linq; class Program { static void Main() { int[] array = { 1, 3, 5, 7 }; Console.WriteLine(array.Average()); } } Output 4