在一个地方我正在使用字符串列表,在这种情况下,我可以更改字符串的值,如下所示,
foreach(string item in itemlist.ToList())
{
item = someValue; //I am able to do this
}
但是对于类的对象,我无法更改对象的成员值,代码如下,
public class StudentDTO
{
string name;
int rollNo;
}
studentDTOList=GetDataFromDatabase();
foreach(StudentDTO student in studentDTOList.ToList())
{
student = ChangeName(student); //Not working
}
private StudentDTO ChangeName(StudentDTO studentDTO)
{
studentDTO.name = SomeName;
return studentDTO;
}
错误是:无法分配,因为它是迭代变量