Use anonymous methods

Anonymous methods is essentially a way, but he did not name any place using a delegate variable assignment method can be used anonymously

Method Code:

 

static void doubleint(int t1, int t2)
        {
            Console.WriteLine(t1+t2);
        }

 

Anonymous method of the form:

Func<int, int, int> f = delegate(int t1, int t2)
            {
                return t1 + t2;
            };

The above is an example of a built-Func delegate, delegate any use local variables can be used

Lamba expression:

Starting from c # 3.0, instead of using anonymous methods Lambda expressions, so Lambda expressions also declares a method, any place using a delegate variable can be used, an anonymous method can, he can.

Format is as follows:

  Func<int, int, int> a = (arg1, arg2) =>
            {
                return arg1 + arg2;
            };

When only one, and only one function body Lambda parameter representation, it can be used without braces and braces function body parameters

Func<int,int> b = arg => arg+15;
Func<int, int> c = (arg) =>
            {
                return arg + 15;
            };
            Console.WriteLine(b(35));
            Console.WriteLine(c(35));
            Console.ReadKey();

Thus both returned result should be the same, are both 50.

Note: You can access via Lambda expressions to variables outside of it, this is a nice feature, but sometimes very dangerous.

 

 

 

Guess you like

Origin www.cnblogs.com/thebrave/p/11909556.html