The past and present of Lambda expressions ~~~~~~, Lambda past and present
Namespace MyLamdba
{
Class Program
{
Static void Main (string [] args)
{
LamDbaShow. Show ();
Console. ReadKey ();
}
}
}
========================================================== ========================================================== ==========================================
Namespace MyLamdba
{
Public delegate void OutNoResultNoPara (); // No Return Value
Public delegate void OutNoResultWithPara (string name, DateTime now );//
Public delegate int WithResultWithPara (int x, int y );
Public delegate string NoParaWithResult ();
Public class LamDbaShow
{
Public static void Show ()
{
// Anonymous function. The method name is omitted here. The passing parameter is taken directly and the method name is removed.
OutNoResultWithPara method1 = new OutNoResultWithPara (
Delegate (string name, DateTime now)
{
Console. WriteLine ("I am {0}, now {1} has clicked", name, now );
}
);
Method1.Invoke ("aaaa", DateTime. Now );
// ============================================^^^^==^^^= ^ I am a beautiful split line...... ^ = ========================
OutNoResultWithPara method2 = new OutNoResultWithPara (
(String name, DateTime now) => // Replace the delegate keyword with => the parameter list on the left of the arrow and the method body on the right.
{
Console. WriteLine ("I am {0}, now {1} has clicked", name, now );
}
);
Method2.Invoke ("aaaa", DateTime. Now );
// The lamdba expression is actually an anonymous method.
// ============================================^^^^==^^^= ^ I am a beautiful split line...... ^ = ========================
OutNoResultWithPara method3 = new OutNoResultWithPara (
(Name, now) => // Replace the delegate keyword with => the parameter list on the left of the arrow, and the method body on the right.
{
Console. WriteLine ("I am {0}, now {1} has clicked", name, now );
}
); // The parameter type is removed, because the type of the input parameter is restricted during delegation.
Method3.Invoke ("aaaa", DateTime. Now );
// ============================================^^^^==^^^= ^ I am a beautiful split line...... ^ = ========================
OutNoResultWithPara method4 = (name, now) =>
{
Console. WriteLine ("I am {0}, now {1} has clicked", name, now );
}
); // Removed the instantiation delegate and directly passed the Parameter
Method3.Invoke ("aaaa", DateTime. Now );
// ============================================^^^^==^^^= ^ I am a beautiful split line...... ^ = ========================
OutNoResultWithPara method = (s, d) => Console. WriteLine ("I am {0}, now {1} clicked", s, d );
// Remove the braces, provided that the method has only one row
Method. Invoke ("pig", DateTime. Now );
// ============================================^^^^==^^^= ^ I am a beautiful split line...... ^ = ========================
OutNoResultNoPara met = () => Console. WriteLine ("132123"); //
Met. Invoke ();
// ============================================^^^^==^^^= ^ I am a beautiful split line...... ^ = ========================
WithResultWithPara WW = (x, y) =>{ return x + y ;};
Console. WriteLine (WW. Invoke (1, 2 ));
WithResultWithPara WS = (x, y) => x + y;
Console. WriteLine (WS. Invoke (1, 2); // remove the braces. Remove the return value directly if the method body has only one row,
// ============================================^^^^==^^^= ^ I am a beautiful split line...... ^ = ========================
OutNoResultNoPara sss = () =>{}; // No parameter, no return value, nothing to do
NoParaWithResult aa = () => "I have no parameters and return values ";
Console. WriteLine (aa ());
}
Public static void Study (string name, DateTime now)
{
Console. WriteLine ("I am {0}, now {1} has clicked", name, now );
}
}
}