Upcoming SlideShare
Loading in …5
×

# Lambda expressions

910 views
875 views

Published on

0 Comments
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

No Downloads
Views
Total views
910
On SlideShare
0
From Embeds
0
Number of Embeds
52
Actions
Shares
0
Downloads
26
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Lambda expressions

1. 1. Lambda Expressions<br />How to…<br />YuriySeniuk<br />http://yuriyseniuk.blogspot.com/<br />
2. 2. Why Lambda Expressions?<br />Lambda expressions are simply methods;<br />Give a shorter form of a methods;<br />Speed up our work.<br />
3. 3. Using a Delegate<br />public delegate int Arithmetic(int x);<br />static public int Square(int x)<br />{    return x * x;<br />}<br />Arithmetic myOperation= new Arithmetic (Square);<br />Console.WriteLine("{0}", myOperation(5));<br />
4. 4. Using an Anonymous Methods<br />public delegate int Arithmetic(int x);<br />Arithmetic myOperation= new Arithmetic (<br /> delegate<br /> {<br />return x * x;<br /> });<br />Console.WriteLine("{0}", myOperation(5));<br />
5. 5. public delegate int Arithmetic(int x);<br />Arithmetic myOperation= x => x * x;<br />Console.WriteLine("{0}", myOperation(5));<br />//Also with explicit input type<br />Arithmetic myOperation = (int)x => x * x;<br />Using a Lambda Expressions<br />
6. 6. Using λ with Two Arguments<br />public delegate int Mul(int x, int y);<br />MulmyOperation= (x, y) => x * y;<br />Console.WriteLine("{0}", myOperation(5, 6));<br />//Also with explicit input type <br />MulmyOperation = (int x, int y) => x * y;<br />Console.WriteLine("{0}", myOperation(5, 6));<br />
7. 7. Using λ with Zero Arguments<br />public delegate long Ticks();<br />Ticks myOperation= () => DateTime.Now.Ticks;<br />Console.WriteLine("{0}", myOperation());<br />
8. 8. public delegate void DoNothing();<br />DoNothingmyOperation= () => {};<br />Using λ returns void<br />
9. 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);<br />Complex body<br />
10. 10. How to pronounce<br />If λ is a predicate x => x == “Predicate”<br />=> spoken as “such that” <br />Ex. x such that x state equals Predicate<br />If λ is a projection returns a new type<br />x => newSomeObject(x) <br />=> Spoken as “becomes”<br />Ex. x becomes new some object with name x.<br />
11. 11. The Func Delegate Types<br />public delegate TR Func<TR>();<br />public delegate TR Func<T0, TR>(T0 a0);<br />public delegate TR Func<T0, T1, TR>(T0 a0, T1 a1);<br />public delegate TR Func<T0, T1, T2, TR>(T0 a0, T1 a1, T2 a2);<br />public delegate TR Func<T0, T1, T2, T3, TR>(T0 a0, T1 a1, T2 a2, T3 a3);<br />publicList<T> FilterList<T>( List<T> source, Func<T, bool> filter )<br /> {<br />List<T> list = new List<T>();<br />foreach( T l in source )<br /> {<br /> if ( filter( l ) ) list.Add( l );<br /> }<br /> return list;<br /> } <br />List<int> list = new List<int>() { 3, 5, 7, 9, 5 };<br />List<int> filteredList = FilterList<int>( list, x => x > 5 );<br />
12. 12. The Action Delegate Types<br />public delegate voidAction();<br />public delegate voidAction<T0>(T0 a0);<br />public delegate voidAction<T0, T1>(T0 a0, T1 a1);<br />public delegate voidAction<T0, T1, T2>(T0 a0, T1 a1, T2 a2);<br />Public delegate voidAction<T0, T1, T2, T3>(T0 a0, T1 a1, T2 a2, T3 a3);<br />void DoSomethingWithDelay(Action execute)<br />{ Thread.Sleep(1000); execute();<br />} <br />void F()<br />{DoSomethingWithDelay( () => { Console.WriteLine("Done"); } );<br />}<br />
13. 13. Any questions..<br />?<br />
14. 14. Source<br />http://blogs.msdn.com/b/ericwhite/archive/2006/10/03/lambda-expressions.aspx<br />http://msdn.microsoft.com/en-us/library/bb397687.aspx<br />