A Lambda expression is an anonymous function that enables you to create delegates or expression tree types.
By using lambda expression, you can write local function that can be passed as arguments or returned as the value of function calls.
It is very useful in place where a method is being used only once and the method definition is short.
It saves time to declaring and writing a separate method.
Syntax:
(input parameters) => expression
Lambda Expression:
The operation “=>” is called lambda operator. You can read as “goes to”.
Lambda express has left and right side.
x => x*x
So, here you can see x goes to x*x. where left side x is input parameter x*x is the code execution block.
Example:
using System; class Program { delegate int del(int x, int y); static void Main() { del myFunct = (x, y) => x * y; int result1 = myFunct(2, 3); Console.WriteLine("Using Delegate: {0}", result1.ToString()); } }
Output:
Using Delegate: 6
Press any key to continue…
In above example we have create delegate using delegate keyword with two parameters x and y.
Now this delegate passed x and y value and get result as integer and display it to console screen.
Let’s take an example and compare with class:
using System; class Program { delegate int del(int x, int y); static class myClass { public static int del(int x, int y) { return x * y; } } static void Main() { del myFunct = (x,y) => x*y; int result1 = myFunct(2,3); Console.WriteLine("Using Delegate: {0}", result1.ToString()); int result2 = myClass.del(2, 3); Console.WriteLine("Using Class: {0}", result2.ToString()); } }
Output:
Using Delegate: 6
Using Class: 6
In above example you can see that we have created delegate and static class. Class has static int del method which returns multiplication of x and y.
Different between lambda expression and using class you can see we have return much lines to get result as same lambda expression gives.
So if your code of line is short then you can use lambda expression to get result with single line.
A statement lambda resembles an expression lambda except that the statement(s) is enclosed in braces.
(input parameters) => { statement block };
You can write number of statement to the statement block but typically more than two is good practice.
Statement lambda is like a anonymous methods and which cannot be used to create expression trees.
Let’s take an example of Statement Lambda:
using System; class Program { delegate void del(int x, int y); static void Main() { del myFunct = (x, y) => { string s = "Multiplication of X and Y is: " + x * y; Console.WriteLine(s); }; myFunct(2,3); } }
Output:
Multiplication of X and Y is: 6
Press any key to continue…
Using Func delegates are very useful for encapsulating user defined expression that is applied to each element in a set of source data.
Example:
using System; class Program { delegate TResult Func<TArg0, TResult>(TArg0 arg0); delegate bool Func(int x); static void Main() { Func<int, bool> myf = x => x == 5; bool result = myf(4); Console.WriteLine("Result: {0}", result); Func<int, int> myf1 = x => x * 5; int result2 = myf1(4); Console.WriteLine("Result: {0}", result2); } }
Output:
Result: False
Result: 20
Press any key to continue...
In above example you can see that we have crated Func<TArg0, TResult> delegate. It takes TArge0 as argument and TResult as parameter.
Standard Query Operation Methods:
using System; using System.Linq; class Program { static void Main() { int[] numbers = { 4, 1, 5, 5, 8, 7, 9, 10 }; int odd = numbers.Count(r => r % 2 != 0); Console.WriteLine("Odd Number: {0}", odd); } }
Output:
Odd Number: 5
Notes:
Lambda expression should be short.
Lambda expression is useful with LINQ.
you can also visit: www.mybook-lang.com
No comments:
Post a Comment