SlideShare a Scribd company logo
Lambda ExpressionsHow to…YuriySeniukhttp://yuriyseniuk.blogspot.com/
Why Lambda Expressions?Lambda expressions are simply methods;Give a shorter form of a methods;Speed up our work.
Using a Delegatepublic delegate int Arithmetic(int x);static public int Square(int x){    return x * x;}Arithmetic myOperation= new Arithmetic (Square);Console.WriteLine("{0}", myOperation(5));
Using an Anonymous Methodspublic delegate int Arithmetic(int x);Arithmetic myOperation= new Arithmetic (		delegate			{return x * x;			});Console.WriteLine("{0}", myOperation(5));
public delegate int Arithmetic(int x);Arithmetic myOperation= x => x * x;Console.WriteLine("{0}", myOperation(5));//Also with explicit input typeArithmetic myOperation = (int)x => x * x;Using a Lambda Expressions
Using λ with Two Argumentspublic delegate int Mul(int x, int y);MulmyOperation= (x, y) => x * y;Console.WriteLine("{0}", myOperation(5, 6));//Also with explicit input type	MulmyOperation = (int x, int y) => x * y;Console.WriteLine("{0}", myOperation(5, 6));
Using λ with Zero Argumentspublic delegate long Ticks();Ticks myOperation= () => DateTime.Now.Ticks;Console.WriteLine("{0}", myOperation());
public delegate void DoNothing();DoNothingmyOperation= () => {};Using λ returns void
int[] source = new[] { 3, 8, 4, 6, 1, 7, 9, 2, 4, 8 }; foreach (int i in source.Where(        x =>        {            if (x % 2 == 0)                return true;            else if (x >= 7)                return true;            return false;        }    ))    Console.WriteLine(i);Complex body
How to pronounceIf λ is a predicate x => x == “Predicate”=> spoken as “such that” Ex. x such that x state equals PredicateIf λ is a projection returns a new typex => newSomeObject(x) => Spoken as “becomes”Ex. x becomes new some object with name x.
The Func Delegate Typespublic delegate TR Func<TR>();public delegate TR Func<T0, TR>(T0 a0);public delegate TR Func<T0, T1, TR>(T0 a0, T1 a1);public delegate TR Func<T0, T1, T2, TR>(T0 a0, T1 a1, T2 a2);public delegate TR Func<T0, T1, T2, T3, TR>(T0 a0, T1 a1, T2 a2, T3 a3);publicList<T> FilterList<T>( List<T> source, Func<T, bool> filter )    {List<T> list = new List<T>();foreach( T l in source )      {        if ( filter( l ) ) list.Add( l );      }      return list;    } List<int> list = new List<int>() { 3, 5, 7, 9, 5 };List<int> filteredList = FilterList<int>( list, x => x > 5 );
The Action Delegate Typespublic delegate voidAction();public delegate voidAction<T0>(T0 a0);public delegate voidAction<T0, T1>(T0 a0, T1 a1);public delegate voidAction<T0, T1, T2>(T0 a0, T1 a1, T2 a2);Public delegate voidAction<T0, T1, T2, T3>(T0 a0, T1 a1, T2 a2, T3 a3);void DoSomethingWithDelay(Action execute){ Thread.Sleep(1000); execute();} void F(){DoSomethingWithDelay( () => { Console.WriteLine("Done"); } );}
Any questions..?
Sourcehttp://blogs.msdn.com/b/ericwhite/archive/2006/10/03/lambda-expressions.aspxhttp://msdn.microsoft.com/en-us/library/bb397687.aspx

More Related Content

PPT
Array Presentation (EngineerBaBu.com)
PPTX
Linq inside out
PPTX
Array within a class
PPTX
Lesson 1
PPTX
Stack of Data structure
PPTX
Edsc 304 lesson 1
PPT
PPTX
Stack Data Structure
Array Presentation (EngineerBaBu.com)
Linq inside out
Array within a class
Lesson 1
Stack of Data structure
Edsc 304 lesson 1
Stack Data Structure

What's hot (20)

PDF
Lec 6 14_aug [compatibility mode]
DOC
Ds lab manual by s.k.rath
PDF
STACK ( LIFO STRUCTURE) - Data Structure
PDF
Data structures stacks
PDF
Monad Transformers - Part 1
PPT
Data structure lecture7
PPT
PDF
C# quick ref (bruce 2016)
PPTX
STACKS IN DATASTRUCTURE
PPT
Doublylinklist
PPTX
Template C++ OOP
PPT
PPTX
Templates
PPT
Stack Implementation
PPT
Fundamentals of data structures
PPT
Stacks
PPTX
Templates in C++
PPTX
Lecture 6: linked list
PPT
Lec 6 14_aug [compatibility mode]
Ds lab manual by s.k.rath
STACK ( LIFO STRUCTURE) - Data Structure
Data structures stacks
Monad Transformers - Part 1
Data structure lecture7
C# quick ref (bruce 2016)
STACKS IN DATASTRUCTURE
Doublylinklist
Template C++ OOP
Templates
Stack Implementation
Fundamentals of data structures
Stacks
Templates in C++
Lecture 6: linked list
Ad

Viewers also liked (20)

PPTX
Lambda expressions
PPT
My first experience with lambda expressions in java
PDF
Lambda Expressions in Java 8
PDF
Java Logging discussion Log4j,Slf4j
PPTX
Java 8 lambdas expressions
PDF
Simplifying java with lambdas (short)
PPTX
Lambda Expressions in Java 8
PDF
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
PPTX
Fun with lambda expressions
PDF
Lambda: A Peek Under The Hood - Brian Goetz
PPTX
SLF4J Explained........
PDF
Lambda Expressions in Java
PDF
Programming with Lambda Expressions in Java
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
PPTX
Lambda expressions
PPTX
PPTX
Java 8 lambda
PDF
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
PDF
Productive Programming in Java 8 - with Lambdas and Streams
PDF
Java 8 Lambda Expressions & Streams
Lambda expressions
My first experience with lambda expressions in java
Lambda Expressions in Java 8
Java Logging discussion Log4j,Slf4j
Java 8 lambdas expressions
Simplifying java with lambdas (short)
Lambda Expressions in Java 8
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
Fun with lambda expressions
Lambda: A Peek Under The Hood - Brian Goetz
SLF4J Explained........
Lambda Expressions in Java
Programming with Lambda Expressions in Java
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Lambda expressions
Java 8 lambda
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Productive Programming in Java 8 - with Lambdas and Streams
Java 8 Lambda Expressions & Streams
Ad

Similar to Lambda expressions (20)

PPS
Let Us Learn Lambda Using C# 3.0
PPT
Csharp4 delegates lambda_and_events
ODP
Can't Dance The Lambda
PPTX
Evolution of C# delegates
PPT
Mixing Functional and Object Oriented Approaches to Programming in C#
PPT
Mixing functional and object oriented approaches to programming in C#
PDF
Linq & lambda overview C#.net
PPTX
C# Delegates
PPTX
Fun with lambda expressions
DOCX
Mastering C# Lambda Expressions: A Complete Guide
PPTX
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PPT
Whats New In C# 3.0
PDF
Delegate
PPTX
Delegetes in c#
PPT
PPTX
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
PPTX
Lambdas and Extension using VS2010
PPTX
It's Java Jim, But Not As We Know It!
PPT
C sharp part2
PPTX
Explain Delegates step by step.
Let Us Learn Lambda Using C# 3.0
Csharp4 delegates lambda_and_events
Can't Dance The Lambda
Evolution of C# delegates
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing functional and object oriented approaches to programming in C#
Linq & lambda overview C#.net
C# Delegates
Fun with lambda expressions
Mastering C# Lambda Expressions: A Complete Guide
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
Whats New In C# 3.0
Delegate
Delegetes in c#
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambdas and Extension using VS2010
It's Java Jim, But Not As We Know It!
C sharp part2
Explain Delegates step by step.

Lambda expressions

  • 2. Why Lambda Expressions?Lambda expressions are simply methods;Give a shorter form of a methods;Speed up our work.
  • 3. Using a Delegatepublic delegate int Arithmetic(int x);static public int Square(int x){    return x * x;}Arithmetic myOperation= new Arithmetic (Square);Console.WriteLine("{0}", myOperation(5));
  • 4. Using an Anonymous Methodspublic delegate int Arithmetic(int x);Arithmetic myOperation= new Arithmetic ( delegate {return x * x; });Console.WriteLine("{0}", myOperation(5));
  • 5. public delegate int Arithmetic(int x);Arithmetic myOperation= x => x * x;Console.WriteLine("{0}", myOperation(5));//Also with explicit input typeArithmetic myOperation = (int)x => x * x;Using a Lambda Expressions
  • 6. Using λ with Two Argumentspublic delegate int Mul(int x, int y);MulmyOperation= (x, y) => x * y;Console.WriteLine("{0}", myOperation(5, 6));//Also with explicit input type MulmyOperation = (int x, int y) => x * y;Console.WriteLine("{0}", myOperation(5, 6));
  • 7. Using λ with Zero Argumentspublic delegate long Ticks();Ticks myOperation= () => DateTime.Now.Ticks;Console.WriteLine("{0}", myOperation());
  • 9. int[] source = new[] { 3, 8, 4, 6, 1, 7, 9, 2, 4, 8 }; foreach (int i in source.Where(        x =>        {            if (x % 2 == 0)                return true;            else if (x >= 7)                return true;            return false;        }    ))    Console.WriteLine(i);Complex body
  • 10. How to pronounceIf λ is a predicate x => x == “Predicate”=> spoken as “such that” Ex. x such that x state equals PredicateIf λ is a projection returns a new typex => newSomeObject(x) => Spoken as “becomes”Ex. x becomes new some object with name x.
  • 11. The Func Delegate Typespublic delegate TR Func<TR>();public delegate TR Func<T0, TR>(T0 a0);public delegate TR Func<T0, T1, TR>(T0 a0, T1 a1);public delegate TR Func<T0, T1, T2, TR>(T0 a0, T1 a1, T2 a2);public delegate TR Func<T0, T1, T2, T3, TR>(T0 a0, T1 a1, T2 a2, T3 a3);publicList<T> FilterList<T>( List<T> source, Func<T, bool> filter ) {List<T> list = new List<T>();foreach( T l in source ) { if ( filter( l ) ) list.Add( l ); } return list; } List<int> list = new List<int>() { 3, 5, 7, 9, 5 };List<int> filteredList = FilterList<int>( list, x => x > 5 );
  • 12. The Action Delegate Typespublic delegate voidAction();public delegate voidAction<T0>(T0 a0);public delegate voidAction<T0, T1>(T0 a0, T1 a1);public delegate voidAction<T0, T1, T2>(T0 a0, T1 a1, T2 a2);Public delegate voidAction<T0, T1, T2, T3>(T0 a0, T1 a1, T2 a2, T3 a3);void DoSomethingWithDelay(Action execute){ Thread.Sleep(1000); execute();} void F(){DoSomethingWithDelay( () => { Console.WriteLine("Done"); } );}