I have a search form that executes queries returning lists of objects that are sub-classes of a Contact class.
When the lists are used in gridviews, properties that only exist in sub-classes (such as HireDate) are not displayed because the list contains objects of the base class (Contact).
Is there a way to make GetContacts in the sub-class return a list of Employee instead of a list of Contact ? Or a way to "cast" the list of Contact into a list of Employee ?
Thanks in advance !
public abstract class Contact
{
public string Name { get; set; }
}
public class Employee : Contact
{
public DateTime HireDate { get; set; }
}
public abstract class ContactManager
{
public abstract List<Contact> GetContacts(string searchValue);
}
public class EmployeeManager : ContactManager
{
public abstract List<Contact> GetContacts(string searchValue);
}
Yes, generics can help here:
Alternatively, you can use the LINQ OfType method to get all contacts of a desired type from your collection: