110,567
社区成员
发帖
与我相关
我的任务
分享
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student() {Name ="1111",Adress ="aaaa1"},
new Student() {Name ="2222",Adress ="aaaa2"}
};
Student s = new Student()
{
Name = "cccccc",
Adress = "dddddd"
};
students.ForEach(m =>
{
m = s;
});
}
}
public class Student
{
public string Name { get; set; }
public string Adress { get; set; }
}
m = s;
并不是什么属性赋值,这是给变量(引用)赋值。
这部分也先要搞清楚“赋值”的含义。for (int i = 0; i < students.Count; i++)
{
students[i] = s;
}
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student() {Name ="1111",Adress ="aaaa1"},
new Student() {Name ="2222",Adress ="aaaa2"}
};
Student s = new Student()
{
Name = "cccccc",
Adress = "dddddd"
};
for (var i = 0; i < students.Count; ++i)
students[i] = s;
}