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 #