Search for operator overview '=>

I am looking at a sample asp.net code in an MVC project and need a better understanding of the => operator. Including => in search engines is not useful.

THX

+3
source share
3 answers

The syntax =>creates lambda expressions , which are small functions.

For example, the line

Func<int, int> myFunc = i => 2 * i;

declares a type variable Func<int, int>(a delegate that takes one integer and returns another), and assigns it lambda expressions that take a parameter with a name i(the compiler automatically determines that ia int) and returns 2 * i.

+5
source

= > , , -. . :

Person , , Where() Func, :

    Func<Person, bool> isMale = delegate(Person peep) { return peep.Gender == "male"; };
    var men = from p in peeps.Where(isMale)
              select p;

:

    var women = from p in peeps.Where(delegate(Person peep) { return peep.Gender != "male"; })
                select p;

, , :

    var women = from p in peeps.Where(x => x.Gender != "male")
                select p;

delegate(Person peep) x 'return peep.Gender!= "male" and 'x.Gender != "male".

+3

Source: https://habr.com/ru/post/1727028/


All Articles