The past and present of Lambda expressions ~~~~~~, Lambda past and present

Source: Internet
Author: User

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 );
}

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.