با سلام .
فرض کنید 2 کلاس داریم که از یک کلاس والد مشتق شده اند. :
public class Person
}
public Person() { }
public Person(string name) { FirstName = name; }
public Person(string firstname, string lastname) { FirstName = firstname; LastName = lastname; }
public Person(string firstname, string lastname, int age) { FirstName = firstname; LastName = lastname; Age = age; }
public Person(Person person) { FirstName = person.FirstName; LastName = person.LastName; Age = person.Age; }
public Person(Student student) { FirstName = student.FirstName; LastName = student.LastName; Age = student.Age; }
public Person(Teacher teacher) { FirstName = teacher.FirstName; LastName = teacher.LastName; Age = teacher.Age; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
{
///////////////////////////////////////////////////////////////////////////////////////////////////
public class Student : Person
}
public int Grade { get; set; }
public Student() { }
public Student(string name) { FirstName = name; }
public Student(string firstname, string lastname) { FirstName = firstname; LastName = lastname; }
public Student(string firstname, string lastname, int age) { FirstName = firstname; LastName = lastname; Age = age; }
public Student(string firstname, string lastname, int age, int grade) { FirstName = firstname; LastName = lastname; Age = age; Grade = grade; }
public Student(Person person) { FirstName = person.FirstName; LastName = person.LastName; Age = person.Age; }
public Student(Student person) { FirstName = person.FirstName; LastName = person.LastName; Age = person.Age; Grade = person.Grade; }
public Student(Teacher person) { FirstName = person.FirstName; LastName = person.LastName; Age = person.Age; }
public static implicit operator Student(Teacher v)
{
return new Student(v);
}
{
///////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Teacher : Person
}
public string School { get; set; }
public Teacher() { }
public Teacher(string name) { FirstName = name; }
public Teacher(string firstname, string lastname) { FirstName = firstname; LastName = lastname; }
public Teacher(string firstname, string lastname, int age) { FirstName = firstname; LastName = lastname; Age = age; }
public Teacher(string firstname, string lastname, int age, string school) { FirstName = firstname; LastName = lastname; Age = age; School = school; }
public Teacher(Person person) { FirstName = person.FirstName; LastName = person.LastName; Age = person.Age; }
public Teacher(Teacher person) { FirstName = person.FirstName; LastName = person.LastName; Age = person.Age; School = person.School; }
public Teacher(Student person) { FirstName = person.FirstName; LastName = person.LastName; Age = person.Age; }
public static implicit operator Teacher(Student v)
{
return new Teacher(v);
}
{
حالا چطور میوانم یک لیست از کلاس مشتق شده اول را به یک لیست از کلاس مشتق شده دوم تبدیل کنم؟
یعنی یک چیزی مثل این :
List<Student> students = new List<Student>
{
new Student{FirstName ="st1"},
new Student { FirstName = "st2", LastName = "Me", Age = 34, Grade = 12 }
};
List<Teacher> teachers = new List<Teacher>();
teachers = students;
البته این را هم بگم که teachers = students.OfType<Teacher>().ToList(); را هم امتحان کردم اما هیچ لیستی برگشت داده نشد .
این پست در تاریخ {{ dateString(new Date(post.deleteDate)) }} توسط {{ post.deletedByUser }} حذف شده است.
دلیل حذف: {{ post.deleteReason ?? 'نامشخص' }}
زمان پاسخ گویی روز های شنبه الی چهارشنبه ساعت 9 الی 18
فقط به موضوعات مربوط به محصولات آموزشی و فروش پاسخ داده می شود