Part of the Func delegate in C #

Source: Internet
Author: User

What's func<t,tresult> in C #?

In simple terms,func<t,tresult> is just generic delegate. Depending on the requirement,the type parameters (T and TResult) can is replaced with the corresponding (corresponding) type arguments .

For example,func<employee,string> are a delegate that represents (rep) a function expecting (expect) Employee object as an I Nput parameter and returns a string.

classProgram {Static voidMain (string[] args) {List<Employee> employees =NewList<employee>() {                 NewEmployee{id=101, name="lin1"},                NewEmployee{id=102, name="lin2"},                NewEmployee{id=103, name="lin3"}            }; //Func<employee, string> selector = e = "Name=" + e.name;ienumerable<string> employeenames = employees. Select (E ="name="+e.name); foreach(stringNameinchemployeenames)            {Console.WriteLine (name); }        }    }    classEmployee { Public intID {Get;Set; }  Public stringName {Get;Set; } }
View Code

What is the difference between Func delegate and lambda expression?

They ' re the same, just, different ways to write the same thing. The lambda syntax is newer, more concise (concise) and easy to write.

What if I has to pass II or more input parameters?

As of this recording there is overloaded versions of Func, which enables us to pass variable number and type of input Parameters. In the example below, func<int,int,string> represents a function that expects 2 int input parameters and returns a S Tring.

 class program {s Tatic void Main (string [] args) {func<int, int, string> funcdelegate = (NUM1, num2) = (num1 + num2) . ToString (); string result = Funcdelegate (10,20); Console.WriteLine ( "sum=  "+result);}       

Finally, I completely learning the C # language, go to bed, good night guy.

Part of the Func delegate in C #

Related Article

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.