How can I write a Func expression without a Lambde expression?

I just think how to do it: List.Where(X=>X>5); to the non-lambda code of the expression. I can't figure out how to get Func to work here.

+4
source share
2 answers

There are two fairly simple options for creating delegates without using lambda expressions:

  • Write a method and use method group transform

     private static bool GreaterThan5(int x) { return x > 5; } ... var query = list.Where(GreaterThan5); 
  • Use anonymous method

     var query = list.Where(delegate(int x) { return x > 5; }); 

None of them are as clear as using the lambda expression. For more complex examples, when you really want to capture local variables, the "write a separate method" version will be much more complicated.

+12
source

Until I understand the purpose of this, you can do it as follows:

 bool MyFilterFunction(int x) { return x > 5; } 

Then rewrite the code:

 List.Where(MyFilterFunction); 
+3
source

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


All Articles