Transfer from Pnljs delegate (func<int,bool>)

Source: Internet
Author: User

Often see Func<int, bool> This way of writing, see This is no mind to look down. We still need to be quiet when we learn technology.

The Func to func<int,bool> goes to the definition to see its explanation:

//Summary:
   //     Encapsulates a method that has one parameter and returns the type value specified by the TResult parameter.
   //
   //Parameters:
   //   arg:
    //     The parameters of the method encapsulated by this delegate.
   //
   //Type parameters:
   //   T:
    //     The parameter type of the method encapsulated by this delegate.
   //
   //   TResult:
   //      The return value type of the method encapsulated by this delegate.
   //
   //Return results:
   //     The return value of the method encapsulated by this delegate.
    [Typeforwardedfrom ("System.core, version=3.5.0.0, Culture=neutral, publickeytoken= b77a5c561934e089 ")]
    public delegate TResult Func<in T, out tresult> (t arg);

In t represents the input parameter 1 out TResult represents the output parameter 2 again see the return value is TResult 3 constructor method required parameter is T 4

1 Compare with 4,2 and 3, what did you find?!         Parameter type, right. 5

Func is a delegate, the delegate can be stored inside the method, then we have to build a matching method: Take func<int,bool> as an example:

private bool IsNumberLessThen5 (int number)

{return number < 5;}

Func<int,bool> f1 = IsNUmberLessThen5;

Call: BOOL flag= F1 (4);

Here is the specific code:

Using System; Using System.Collections.Generic; Using System.Linq; Using System.Linq.Expressions; Using System.Text; Using System.Threading.Tasks;

Namespace ConsoleApplication1 {class Program {static void Main (string[] args) {Func             <int, bool> f1 = IsNumberLessThen5;             BOOL flag = f1 (4); Console.WriteLine (flag);

           //The following are other uses, similar to the ISNUMBERLESSTHEN5 function. It's just a different notation.             func<int, bool> f2 = i = i < 5;             func<int, bool> f3 = (i) + = {return I < 5 ; };             Func<int, bool> f4 = delegate (int i) {return I < 5; };             flag = F2 (4); Console.WriteLine (flag);             flag = F3 (4); Console.WriteLine (flag);             flag = f4 (4); Console.WriteLine (flag);            

Console.ReadLine (); }

private static bool IsNumberLessThen5 (int number) {if (number < 5) return true;         return false; }     } }

Transfer from Pnljs delegate (func<int,bool>)

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.