[英]pass lambda expression as callback to an extension method and pass parameter to it
I am trying to understand the concept of lambda expression,extension method,Linq and IEnumerable interface. 我试图理解lambda表达式,扩展方法,Linq和IEnumerable接口的概念。 You can guess that i am farely new to c sharp.Here i've come up with a problem which will incorporate all the above mentioned concepts .
您可以猜测,我对c Sharp还是陌生的。在这里,我提出了一个问题,其中将包含上述所有概念。 Here i have a list which contain three object.I want to change the name property of a Students object in a specified index .I wrote an extension method which accept a callback function.Callback function accepts an integer index and a new Name string.
在这里,我有一个包含三个对象的列表。我想在指定索引中更改Students对象的name属性。我编写了一个扩展方法,该方法接受一个回调函数.Callback函数接受一个整数索引和一个新的Name字符串。 It should change the name property and return the object .But my code failed to do so as i am not sure how to pass parameter to Func callback in extension method.I am in need of some assistant to understand the problem and fix errors from my code ?
它应该更改name属性并返回对象。但是我的代码无法执行此操作,因为我不确定如何在扩展方法中将参数传递给Func回调。我需要一些助手来了解问题并修复我的错误代码?
class Program
{
static void Main(string[] args)
{
List<Students> students = new List<Students>();
students.Add(new Students(111443, "sakib"));
students.Add(new Students(111445, "zami"));
students.Add(new Students(111444, "habib"));
var student = students.First();
var changed1 = students.Change((int num,string newname) => { return students[num].s_name = newname;});
}
}
public class Students
{
public int s_id;
public string s_name;
public Students(int id, string name)
{
this.s_id = id;
this.s_name = name;
}
}
public static class LinqHelper
{
public static IEnumerable<T> Change<T> (this IEnumerable<T> source, Func<int,string,T> callback)
{
var myList = new List<Students>();
myList.Add(callback(1,"zami")); // i was passing parameter here which is not so helpful i guess !
return myList;
}
}
The Func < int, string, T > denotes a function that accepts an integer and string as inputs and T as the return type. Func <int,string,T>表示一个函数,该函数接受整数和字符串作为输入,并接受T作为返回类型。 The anonymous function you have used has a return type of "string":
您使用的匿名函数的返回类型为“字符串”:
var changed1 = students.Change((int num,string newname) => { return students[num].s_name = newname;});
You should return the student instance from the function to make it work. 您应该从函数返回学生实例以使其工作。 Try replacing the above code with the following:
尝试将以下代码替换为以下代码:
var changed1 = students.Change((int index, string newname) =>
{
var studentObj = students[index];
studentObj.s_name = newname;
return studentObj;
});
To allow the LinqHelper to accept the index and argument, use the following: 要允许LinqHelper接受索引和参数,请使用以下命令:
public static class LinqHelper
{
public static IEnumerable<T> Change<T>(this IEnumerable<T> source, Func<int, string, T> callback, int index, string argument)
{
var myList = new List<T>();
myList.Add(callback(index, argument)); // i was passing parameter here which is not so helpful i guess !
return myList;
}
}
And then, you could invoke the method as follows: 然后,您可以按以下方式调用该方法:
var changed1 = students.Change((int index, string newname) =>
{
var studentObj = students[index];
studentObj.s_name = newname;
return studentObj;
},
1,
"zami");
You haven't created the lambda that evaluated to Func<int, string, T>
. 您尚未创建计算为
Func<int, string, T>
的lambda。 Your call to Change extension should look like: 您对Change扩展程序的呼叫应如下所示:
var changed1 = students.Change((num, newnam) => {
students[num].s_name = newnam;
return students[num];
});
(you should return T
as Func
requires). (您应该按照
Func
要求返回T
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.