Concatenating Lambda Functions in C# -


To use C # 3.5 I wanted to make a bill by piece to create a bill to send to pieces where

  Public stable Func & lt; Tran, bool & gt; GetPredicate () {Func & lt; Tran, bool & gt; Predicate = null; Predicate + = t = & gt; T. Response == "00"; Predicate + = t = & gt; T. Quantity & lt; 100; Prediction of return; }  

When I say '=', what does it mean? Predicate - = Nothing does and ^ =, & amp; =, * =, / = Not liked by the compiler.

The editor does not like 'predicate = predicate + t => t Response ....' either.

What have I stumbled? I know what it does, but how is it done?

If anyone wants to hide in more complex lambda, please do so.

"delegate + = method" is the operator for the multicast representative to combine the method for the representative . On the other hand, "delegate - = method" is the operator to remove the method from the delegate. This is useful for verb.

  action action = method 1; Action + = Method 2; Verb + = method 3; Verb = - method2; Action ();  

In this situation, only Method1 and Method3 will run, Method2 will not run because you remove the method before inviting the delegate.

If you are the funk of Moncast Representative, the result will be the final method.

  Funk & lt; Int & gt; Func = () = & gt; 1; Func + = () = & gt; 2; Func + = () = & gt; 3; Int results = func ();  

In this case, the result is 3, because the method "() => 3" is added to the representative in the final representative. Anyway all the method will be called.

In your case, the method "T => T. Amrit and lieutenant; 100" will be effective.

If you want to add predicate, then I suggest these expansion methods.

  Public stable fax & lt; T, bool & gt; Even more & lt; T & gt; (This fx & lt; T, bool & gt; predicate1, funk & lt; t, bool & gt; predicate2) {return arg = & gt; Predicate1 (RG) & amp; Amp; Predicate2 (arg); } Public stable Func & lt; T, bool & gt; Oreal & lt; T & gt; (This fx & lt; T, bool & gt; predicate1, funk & lt; t, bool & gt; predicate2) {return arg = & gt; Predicate1 (Arg) || Predicate2 (arg); }  

Usage

  Public Static Fx & lt; Tran, bool & gt; GetPredicate () {Func & lt; Tran, bool & gt; Predicate = null; Predicate = t = & gt; T. Response == "00"; Predicate = predicate.AndAlso (t = & gt; T. American & lt; 100); Prediction of return; }  

EDIT: Correct the name of the extension methods as suggested by Keith.


Comments