为了解释起见我做了一个例子
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个就足以保持简单并且仍然可以达到目的。请记住,Student和Teacher类包含它们自己的属性,因此根据派生类属性,Base列表中的每个对象都应该不同。
在项目的某个地方,我需要一个返回Base对象列表的方法
List<Base> GetAllLists() {
return allLists; //Lets name the return list of Base objects like this
}
所以我需要将派生类中的对象列表放到Base类List并在某处使用它。
我已经做过类似的事了
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;
}
我收到了评论:
没有必要迭代所有列表并实例化一个新列表 每个实体的对象,因为它们都是从同一个基础继承的 类
那么如何以其他方式实现这一目标呢?
答案 0 :(得分:1)
我想AddRange方法允许您一次性添加所有对象:
var allLists = new List<Base>();
var studentList = new StudentService().GetList();
allLists.AddRange(studentList);
var teacherList = new TeacherService().GetList();
allLists.AddRange(teacherList);
由于Student
和Teacher
继承自base,因此它们可以适合基本List。
这里最大的区别是你只添加参考!这意味着如果您更改原始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));
}
答案 1 :(得分:0)
如果在调用GetAllLists
方法时允许创建列表的新实例,我会执行以下操作
public List<Base> GetAllLists() {
var studentList = new StudentService().GetList();
var teacherList = new TeacherService().GetList();
return studentList.Cast<Base>()
.Concat(teacherList.Cast<Base>())
.ToList();
}
答案 2 :(得分:0)
由于您已经有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