Thursday, 18 October 2012

Lamda Expression simplifies Delegate signatures?

yes, lamda expressions simplifies the code more cleaner, simplifed manner


Delegates are also types, which are predefined , useful to hold the reference of another method.
Delegate declaration.
Delegate defincation.
Delegate innovation.
Ex:
Delegate is a keyword to define a delegate.
delegate int maticalOperation(int x); // Here maticalOperation is Name of the delegate.
maticalOperation add;
// define an delegate type variable [means event] which holds/referes the methods which has same signature like Add delegate.
int sum(int x)
{
            return x+10;
}
So delegate(maticalOperation) can hold/refer the sum operation why because sum operation/method has the same signature of maticalopration signature.
Multicate delegate.
A single delegate can points multiple method references using multicast delegates. In .net most of the delegates are multicast delegates.
Using the signatures
+= and -= operators.
Event based delegates.
Delegates and events are pre-defined in .net we can also define delegates and events for our user defined controls.
For every event we have pre-defined event.// event is a keyword.
Click(object sender, EventArgs e) ---delegate
Button_click(object sender, EventArgs e);---reference of above click delegate.
…like so many delegetes we have in .net.
Delegates are simplifed:
    delegate int maticalOperation(int x);
           maticalOperation m = sum;
            int value=message(5);
            Console.Write(value);
        static int sum(int a)
        {
            return a + a;           
        }
Referenced Method(sum) return type should be delegate(maticalOperation).
Like in lamda Expressions also. Return type should a generic delegate.
Example: Var is not acceptable as return type.
Fun<>,Action<>,predicate<> for developer compatability//instead of created by developer.
Like.

Action<int,int,int> message = (int a,int b,int c)=> //or delegate(int a,int b,int c)
{
  Console.WriteLine("The addition of three number is " + (a + b + c));
};
message(5, 6,71);
and => expression return type must be a generic delegate.
Delegates are simplified with the help of anamous methods and lamda expressions.
// linq makes dynamic memory allocation.
We can call the delegate in asynchronsly also using BeginInvoke method.
Invoke is synchronus method.
Lamda expressions are coupled with generic delegates.
So lamda expressions are tightly coupled with delegates[generic delegates].
So lamda-expressions return type is a delegate(generic delegate).
As well these delegate are can work with any types……..int,string,employee,..ect.
So finally delegates and lamda expressions are developer prospective to make programming more flixable.
Demo1:
delegate int maticalOperation(int x);
maticalOperation m = sum;
int value=m(4);
Console.Write(value);
static int sum(int a)
{
            return a + a;           
}
Here we create the maticalOperation delegate instead of this. We have to use existing delegate is Fun<> which will retun the value like below
Demo 2:
   Func<int, int, int> value1 = delegate(int a, int b)
            {
                return a * b;
            };
            Console.WriteLine("{0}", value1(5, 6));
   
Instead of writing keyword delegate we have simplify this like
Demo 3:
  Func<int, int, int> value1 = (int a, int b)=>
  {
                return a * b;
  };
 Console.WriteLine("{0}", value1(5, 6));   

prefined delegate is : Func<T,T,TResult>()
Demo 4:
   Func<int, int, int> value1 = (int a, int b) => (a * b);
    Console.WriteLine("{0}", value1(5, 6));  

Incase of single variable not require any parathisis like above.
Simplified like this. Here Int type for retrun type is Int***
Ex:
   Func<int,int> value2= a => a+10;
   Console.WriteLine("{0}", value2(4));   

as well as

lamda expression prepares(returns) a proper expression tree as per Lamda Expression of Right side

Expression<delegate> res=x=>x*x;

like
                     delegate int maticalOperation(int x);

            Console.WriteLine(" general way   ");

            maticalOperation myDelegate = x => x * x;
            int j = myDelegate(5);
            Console.WriteLine(j.ToString());

            Console.WriteLine(" 1st way ");

            Expression<maticalOperation> result = a => a * a;
            Console.WriteLine(result.Compile()(4));

            Console.WriteLine(" 2nd way  ");
            Expression<Func<int, int>> second = a => a * a;
            Console.WriteLine(second.Compile()(4));

so finally i want to say: delegates Signatures and return types should match.



Note: For every lamda expression few pre-defined delegates in dotnet framework. like

Func<T>(), Func<T,TResult>().Func<T,T,TResult>()...delegates available. in dotnet framework.




As well delegates usages:

Asynchronus programming.
Callback mechinamsms.
for holding the reference of another method.

Reference:

http://www.dotnetfunda.com/articles/article1956-func-and-action-delegates.aspx


No comments:

Post a Comment