Method for any class derived from a generic base class

st12

Is it possible to have one PrintList() method cover Kid, Student and Person classes?

I don't want to implement PrintList() as a class method into Person or Kid, Student and Person.

public class Person<T>
{
  public T Id { get; set; }
  public string Name { get; set; }
}

public class Kid : Person<byte> { }
public class Student : Person<short> { }
public class Adult : Person<int> { }
/* ... more classes ... */


class Program
{
  static void PrintList(List<T> list) where T : Person  /* NOT POSSIBLE */
  {
    foreach (var item in list)
      Console.WriteLine("Id: {0}, Name: {1}", item.Id, item.Name);
  }

  static void Main(string[] args)
  {
    var students = new List<Student>();
    students.Add(new Student() { Id = 1, Name = "SA" });
    students.Add(new Student() { Id = 2, Name = "SB" });

    var adults = new List<Adult>();
    adults.Add(new Adult() { Id = 1, Name = "AA" });
    adults.Add(new Adult() { Id = 2, Name = "AB" });
    adults.Add(new Adult() { Id = 3, Name = "AC" });

    PrintList(students);
    PrintList(adults);

    Console.ReadLine();
  }
}
Rawling

Well, first of all you need to make the PrintList method generic by adding a type parameter:

static void PrintList<T>(List<T> list) where T : Person /* still doesn't work */

But this still doesn't work, because Person isn't a type; only Person<T> is for some T. We actually need two type arguments:

static void PrintList<T, U>(List<T> list) where T : Person<U>

This works in the definition, but it means that you have to explicitly supply the types when you call the method:

PrintList<Student, short>(students);
PrintList<Adult, int>(adults);

This is workable, but ugly. Can we do better?

Turns out we can. IF you make your signature

static void PrintList<T>(IEnumerable<Person<T>> list)

then you can go back to not supplying any type argument. You lose the ability to treat list like a List inside the method, but you weren't using that anyway.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Returning any derived class type from a method outside the base and derived class

From Dev

Clone derived class from base class method

From Dev

Base class calls method from derived class?

From Dev

Clone derived class from base class method

From Dev

Use generic base class in a collection of classes derived from generic

From Dev

Use generic base class in a collection of classes derived from generic

From Dev

is there any way to prevent the derived class from initializing a base class reference

From Dev

Method called in creator is called from base class but not from derived class

From Dev

Derived class not inheriting overloaded method from base class

From Dev

Calling derived class method from base class pointer

From Dev

Call derived class method from base class instance

From Dev

How to call derived class method from base class pointer?

From Dev

friendship from derived class method to base class members

From Dev

Access to base class method only from the derived class

From Dev

Calling derived class method from base class destructor

From Dev

Calling derived class method from base class destructor

From Dev

Call derived class method from base class instance

From Dev

Using Derived Class In a Base Method

From Dev

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

From Dev

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

From Dev

Method in base class that returns derived class type?

From Dev

Derived class does not call base class method

From Dev

How to call a static method on a base class from a generic class<T>?

From Dev

Get attribute of derived C# class passed as base class to generic method

From Dev

Calling a private base method from a derived class in C#

From Dev

Calling virtual method of base template from derived variadic template class

From Dev

Can I call a derived method from base class?

From Dev

returning derived class from overrided method, declared as returning base copy

From Dev

How to obtain the derived class type from base when calling a method

Related Related

  1. 1

    Returning any derived class type from a method outside the base and derived class

  2. 2

    Clone derived class from base class method

  3. 3

    Base class calls method from derived class?

  4. 4

    Clone derived class from base class method

  5. 5

    Use generic base class in a collection of classes derived from generic

  6. 6

    Use generic base class in a collection of classes derived from generic

  7. 7

    is there any way to prevent the derived class from initializing a base class reference

  8. 8

    Method called in creator is called from base class but not from derived class

  9. 9

    Derived class not inheriting overloaded method from base class

  10. 10

    Calling derived class method from base class pointer

  11. 11

    Call derived class method from base class instance

  12. 12

    How to call derived class method from base class pointer?

  13. 13

    friendship from derived class method to base class members

  14. 14

    Access to base class method only from the derived class

  15. 15

    Calling derived class method from base class destructor

  16. 16

    Calling derived class method from base class destructor

  17. 17

    Call derived class method from base class instance

  18. 18

    Using Derived Class In a Base Method

  19. 19

    Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

  20. 20

    Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

  21. 21

    Method in base class that returns derived class type?

  22. 22

    Derived class does not call base class method

  23. 23

    How to call a static method on a base class from a generic class<T>?

  24. 24

    Get attribute of derived C# class passed as base class to generic method

  25. 25

    Calling a private base method from a derived class in C#

  26. 26

    Calling virtual method of base template from derived variadic template class

  27. 27

    Can I call a derived method from base class?

  28. 28

    returning derived class from overrided method, declared as returning base copy

  29. 29

    How to obtain the derived class type from base when calling a method

HotTag

Archive