30 Days C # foundation consolidation----lambda expression

Source: Internet
Author: User

These days a bit not in the state, every time I want to seriously study, write something when will be a variety of small things delay, execution is too poor. So self-reflection of the recent learning situation, for the basic knowledge, can be learned from the book and video, I still need to pay attention to their own previous projects in the knowledge points, those are the key to HR questions. Today are No. 6.4 number, time is really fast ah, the school about 7.18 Holiday, I intend to see XI ' an, can find an internship, not the words can only wait until the first semester to go school recruit. We can have a lot of classes in the first semester of our senior year, I really don't know how the teacher thinks. So in this still less than 2 months of time, the things you want to learn to study well, improve their executive power and self-control.

delegate, anonymous function, lambda expression

About the commission said before, in fact, is a few steps, but want to be proficient in the use of the project, not so simple, these knowledge points are not too easy to grasp, very abstract, need to through a long period of project code practice can better understand the ideas that the predecessors advocated.

The anonymous function----implement the 1--100.

    <summary>/    //anonymous method/    //</summary> class program    {        //define delegates. Public        delegate int getsumnum (int min,int max);        The static void Main (string[] args)        {            ///anonymous method must be defined using delegate, and the parameters and return values must be the same as the defined delegates.            Getsumnum _getsum = delegate (int min,int max)            {                int sum = 0;                for (int i =min; I <=max; i++)                {                    sum + = i;                                    }                return sum;            };            int a=_getsum (1,100); Call an anonymous function to pass a parameter.            Console.WriteLine (a);            Console.readkey ();        }    }

Using lambda expressions to get results, compare the differences and similarities between the two.

    <summary>/    //anonymous method/    //</summary> class program    {        //define delegates. Public        delegate int getsumnum (int min, int max);        static void Main (string[] args)        {            //lambda expression  Note: If there is a lot of code in the method body, we can write a {} to represent it.            getsumnum _getsum = (min, max)            = {                int sum = 0;                for (int i = min; I <= max; i++)                {                    sum + = i;                }                return sum;            };            int a = _getsum (1, 100); Call an anonymous function to pass a parameter.            Console.WriteLine (a);            Console.readkey ();        }    }

Attention:

When we use lambda expressions, (A, B) =>{}; A, B is our parameter, {} Inside is we need to write the code snippet, if only one line of code is written directly in the following do not need to write {}.

Eg: get the number of daffodils between 1--1000.

Narcissus number refers to an n-digit number (n≥3), and the sum of the number of its digits on each bit is equal to its own.

    Class Program {public delegate bool Getnum (int num);            static void Main (string[] args) {list<int> listnum=new list<int> ();            for (int i = +; I <1000; i++) {listnum.add (i);           }//The lambda method is passed in and executed in Mywhere.          Int[] Ns=mywhere (Listnum.toarray (), E = = {//123 int n1 = E%10;   3 int n2 = e/10%10;     2 int n3 = e/100;            1 return (n1*n1*n1 + n2*n2*n2 + n3*n3*n3) = = e;            });            Console.WriteLine ("The number of daffodils between the 100--1000------:"); for (int i = 0; I <ns. Length;            i++) {Console.WriteLine (ns[i]);        } console.readkey (); }//method of implementing delegate public static int[] Mywhere (int[] num,getnum ck) {list<int> list=new Li            St<int> (); for (int i = 0; i<num. Length; i++) {if (CK (Num[i])) {list.                ADD (Num[i]); }} return list.        ToArray (); }    }

30 Days C # foundation consolidation----lambda expression

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.