为了解释,我做了一个示例
public class Base {
public string Name { get; set; }
public string Surname { get; set; }
public Base(string name, string surname) {
Name = name;
Surname = surname;
}
}
public class Student : Base {
public int StudentID { get; set; }
public Student(string name, string surname, int studentID)
: base(name, surname) {
Name = name;
Surname = surname;
StudentID = studentID;
}
public override string ToString() {
return string.Format("Name is {0}, surname is {1}, ID is {2}",
Name, Surname, StudentID);
}
}
public class Teacher : Base {
public string TeachingSubject { get; set; }
public Teacher(string name, string surname, string teachingSubject)
: base(name, surname) {
Name = name;
Surname = surname;
TeachingSubject = teachingSubject;
}
public override string ToString() {
return string.Format("Name is {0}, surname is {1}, TeachingSubject is {2}",
Name, Surname, TeachingSubject);
}
}
就我而言,有4个儿童课程,但是2足以保持简单并仍然达到重点。请记住,根据派生的类属性,学生和教师课堂包含自己的属性,因此基本列表中的每个对象都应不同。
项目中的某个地方我需要有一种返回基本对象列表
的方法List<Base> GetAllLists() {
return allLists; //Lets name the return list of Base objects like this
}
因此,我需要将来自派生类的对象列表放在基类列表中并在某个地方使用。
我已经做过这样的事情
List<Base> GetAllLists() {
var allLists = new List<Base>();
var studentList = new StudentService().GetList(); //Service class that populates the list of students from a csv file
foreach (var item in studentList) {
allLists.Add(new Student(item.Name, item.Surname, item.StudentID));
}
var teacherList = new TeacherService().GetList(); //Service class that populates the list of teachers from a csv file
foreach (var item in teacherList) {
allLists.Add(new Teacher(item.Name, item.Surname, item.TeachingSubject));
}
return allLists;
}
我收到了评论:
无需迭代所有列表并实例化新的列表 每个实体的对象,因为它们都从同一基础继承 班级
那么如何以其他方式实现这一目标?
我猜addrange方法将允许您一击中所有对象:
var allLists = new List<Base>();
var studentList = new StudentService().GetList();
allLists.AddRange(studentList);
var teacherList = new TeacherService().GetList();
allLists.AddRange(teacherList);
由于Student
和Teacher
从基础继承,它们可以适合基础列表。
这里的最大区别是您仅添加参考!这意味着,如果您更改原始studentList
中的值,它也将在allLists
列表中更改!
虽然在您发布的代码中不会发生这种情况,因为您为studentList
中的每个条目创建一个新实例。这样,您可以在两个列表之间创建独立性。
但是,如果studentList
只是一个未经操纵的本地变量,则使用AddRange
是合法的,并将引用简单地添加到allLists
列表
这是一个小型测试程序。将其粘贴到控制台应用程序中,并亲自查看区别。只需更改copyreference
值并比较结果:
void Main()
{
bool copyreference = true;
var allLists = new List<Base>();
var studentList = new List<Student>();
studentList.Add(new Student("Alf", "Bedrock", 123));
studentList.Add(new Student("Alfine", "Bedrock", 456));
var teacherList = new List<Teacher>();
teacherList.Add(new Teacher("Brad", "Gulp", "MATH"));
teacherList.Add(new Teacher("Evelyn", "Gulp", "BIO"));
if (copyreference)
{
allLists.AddRange(studentList);
allLists.AddRange(teacherList);
}
else
{
foreach (var item in studentList)
{
allLists.Add(new Student(item.Name, item.Surname, item.StudentID));
}
}
Console.WriteLine(String.Join(Environment.NewLine, allLists));
// TEST changing a value in the original list
studentList[0].Name = "Harry";
// if you copied references you will see the change in the final list
Console.WriteLine(Environment.NewLine + String.Join(Environment.NewLine, allLists));
}
我会执行以下操作,如果允许在GetAllLists
方法称为
public List<Base> GetAllLists() {
var studentList = new StudentService().GetList();
var teacherList = new TeacherService().GetList();
return studentList.Cast<Base>()
.Concat(teacherList.Cast<Base>())
.ToList();
}
-
您不需要编写
Name = name; Surname = surname;
,因为您已经有: base(name, surname)
-
关于
AddRange()
中的阴影/深复制,您还可以实现ICloneable
或添加复制构造函数。示例:
类定义:
public class Base {
public string Name { get; set; }
public string Surname { get; set; }
public Base(string name, string surname) {
Name = name;
Surname = surname;
}
}
public class Student : Base, ICloneable { // ICloneable
public int StudentID { get; set; }
public Student(string name, string surname, int studentID) : base(name, surname) {
//Name = name;
//Surname = surname;
StudentID = studentID;
}
public override string ToString() {
return string.Format("Name is {0}, surname is {1}, ID is {2}", Name, Surname, StudentID);
}
public object Clone() {
return MemberwiseClone();
}
}
public class Teacher : Base { // Copy constructor
public string TeachingSubject { get; set; }
public Teacher(string name, string surname, string teachingSubject) : base(name, surname) {
//Name = name;
//Surname = surname;
TeachingSubject = teachingSubject;
}
public override string ToString() {
return string.Format("Name is {0}, surname is {1}, TeachingSubject is {2}", Name, Surname, TeachingSubject);
}
public Teacher(Teacher obj) : base(obj.Name, obj.Surname) {
TeachingSubject = obj.TeachingSubject;
}
}
示例用法:
// AddRange()
List<Student> stud = new List<Student>(2);
stud.Add(new Student("s1", "ss1", 1));
stud.Add(new Student("s2", "ss2", 2));
List<Teacher> teac = new List<Teacher>(2);
teac.Add(new Teacher("t1", "ts1", "subject1"));
teac.Add(new Teacher("t2", "ts2", "subject2"));
List<Base> bas = new List<Base>(4);
bas.AddRange(stud);
bas.AddRange(teac);
Debug.Print($"{Environment.NewLine}AddRange():");
Debug.Print($"Base List:{Environment.NewLine}{bas[0]}{Environment.NewLine}{bas[1]}{Environment.NewLine}{bas[2]}{Environment.NewLine}{bas[3]}");
stud[0] = new Student("s3_changed", "ss3_changed", 3);
stud[1].Name = "s4_changed";
stud[1].Surname = "ss4_changed";
stud[1].StudentID = 4;
bas[2].Name = "b3_changed";
bas[2].Surname = "bs3_changed";
((Teacher)bas[2]).TeachingSubject = "bsub3_changed";
bas[3] = new Teacher("t4_changed", "ts4_changed", "tsbu4_changed");
Debug.Print($"{Environment.NewLine}After Change:");
Debug.Print($"Student List:{Environment.NewLine}{stud[0]}{Environment.NewLine}{stud[1]}");
Debug.Print($"Teacher List:{Environment.NewLine}{teac[0]}{Environment.NewLine}{teac[1]}");
Debug.Print($"Base List:{Environment.NewLine}{bas[0]}{Environment.NewLine}{bas[1]}{Environment.NewLine}{bas[2]}{Environment.NewLine}{bas[3]}");
// Concat()
List<Student> stud2 = new List<Student>(2);
stud2.Add(new Student("s1", "ss1", 1));
stud2.Add(new Student("s2", "ss2", 2));
List<Teacher> teac2 = new List<Teacher>(2);
teac2.Add(new Teacher("t1", "ts1", "subject1"));
teac2.Add(new Teacher("t2", "ts2", "subject2"));
List<Base> bas2 = new List<Base>();
bas2 = bas2.Concat(stud2).Concat(teac2).ToList();
Debug.Print($"{Environment.NewLine}Concat:");
Debug.Print($"Base List:{Environment.NewLine}{bas2[0]}{Environment.NewLine}{bas2[1]}{Environment.NewLine}{bas2[2]}{Environment.NewLine}{bas2[3]}");
stud2[0] = new Student("s3_changed", "ss3_changed", 3);
stud2[1].Name = "s4_changed";
stud2[1].Surname = "ss4_changed";
stud2[1].StudentID = 4;
bas2[2].Name = "b3_changed";
bas2[2].Surname = "bs3_changed";
((Teacher)bas2[2]).TeachingSubject = "bsub3_changed";
bas2[3] = new Teacher("t4_changed", "ts4_changed", "tsbu4_changed");
Debug.Print($"{Environment.NewLine}After Change:");
Debug.Print($"Student List:{Environment.NewLine}{stud2[0]}{Environment.NewLine}{stud2[1]}");
Debug.Print($"Teacher List:{Environment.NewLine}{teac2[0]}{Environment.NewLine}{teac2[1]}");
Debug.Print($"Base List:{Environment.NewLine}{bas2[0]}{Environment.NewLine}{bas2[1]}{Environment.NewLine}{bas2[2]}{Environment.NewLine}{bas2[3]}");
// AddRange() clone
List<Student> stud3 = new List<Student>(2);
stud3.Add(new Student("s1", "ss1", 1));
stud3.Add(new Student("s2", "ss2", 2));
List<Teacher> teac3 = new List<Teacher>(2);
teac3.Add(new Teacher("t1", "ts1", "subject1"));
teac3.Add(new Teacher("t2", "ts2", "subject2"));
List<Base> bas3 = new List<Base>(4);
bas3.AddRange(stud3.Select(x => (Student)x.Clone()).ToList()); // Clone
bas3.AddRange(teac3.Select(x => new Teacher(x)).ToList()); // Copy c'tor
Debug.Print($"{Environment.NewLine}AddRange() clone:");
Debug.Print($"Base List:{Environment.NewLine}{bas3[0]}{Environment.NewLine}{bas3[1]}{Environment.NewLine}{bas3[2]}{Environment.NewLine}{bas3[3]}");
stud3[0] = new Student("s3_changed", "ss3_changed", 3);
stud3[1].Name = "s4_changed";
stud3[1].Surname = "ss4_changed";
stud3[1].StudentID = 4;
bas3[2].Name = "b3_changed";
bas3[2].Surname = "bs3_changed";
((Teacher)bas3[2]).TeachingSubject = "bsub3_changed";
bas3[3] = new Teacher("t4_changed", "ts4_changed", "tsbu4_changed");
Debug.Print($"{Environment.NewLine}After Change:");
Debug.Print($"Student List:{Environment.NewLine}{stud3[0]}{Environment.NewLine}{stud3[1]}");
Debug.Print($"Teacher List:{Environment.NewLine}{teac3[0]}{Environment.NewLine}{teac3[1]}");
Debug.Print($"Base List:{Environment.NewLine}{bas3[0]}{Environment.NewLine}{bas3[1]}{Environment.NewLine}{bas3[2]}{Environment.NewLine}{bas3[3]}");
输出:
AddRange():
Base List:
Name is s1, surname is ss1, ID is 1
Name is s2, surname is ss2, ID is 2
Name is t1, surname is ts1, TeachingSubject is subject1
Name is t2, surname is ts2, TeachingSubject is subject2
After Change:
Student List:
Name is s3_changed, surname is ss3_changed, ID is 3
Name is s4_changed, surname is ss4_changed, ID is 4
Teacher List:
Name is b3_changed, surname is bs3_changed, TeachingSubject is bsub3_changed
Name is t2, surname is ts2, TeachingSubject is subject2
Base List:
Name is s1, surname is ss1, ID is 1
Name is s4_changed, surname is ss4_changed, ID is 4
Name is b3_changed, surname is bs3_changed, TeachingSubject is bsub3_changed
Name is t4_changed, surname is ts4_changed, TeachingSubject is tsbu4_changed
Concat:
Base List:
Name is s1, surname is ss1, ID is 1
Name is s2, surname is ss2, ID is 2
Name is t1, surname is ts1, TeachingSubject is subject1
Name is t2, surname is ts2, TeachingSubject is subject2
After Change:
Student List:
Name is s3_changed, surname is ss3_changed, ID is 3
Name is s4_changed, surname is ss4_changed, ID is 4
Teacher List:
Name is b3_changed, surname is bs3_changed, TeachingSubject is bsub3_changed
Name is t2, surname is ts2, TeachingSubject is subject2
Base List:
Name is s1, surname is ss1, ID is 1
Name is s4_changed, surname is ss4_changed, ID is 4
Name is b3_changed, surname is bs3_changed, TeachingSubject is bsub3_changed
Name is t4_changed, surname is ts4_changed, TeachingSubject is tsbu4_changed
AddRange() clone:
Base List:
Name is s1, surname is ss1, ID is 1
Name is s2, surname is ss2, ID is 2
Name is t1, surname is ts1, TeachingSubject is subject1
Name is t2, surname is ts2, TeachingSubject is subject2
After Change:
Student List:
Name is s3_changed, surname is ss3_changed, ID is 3
Name is s4_changed, surname is ss4_changed, ID is 4
Teacher List:
Name is t1, surname is ts1, TeachingSubject is subject1
Name is t2, surname is ts2, TeachingSubject is subject2
Base List:
Name is s1, surname is ss1, ID is 1
Name is s2, surname is ss2, ID is 2
Name is b3_changed, surname is bs3_changed, TeachingSubject is bsub3_changed
Name is t4_changed, surname is ts4_changed, TeachingSubject is tsbu4_changed