1

I have a problem dealing with Generics.

Some background:

The Animal class is a generified class that needs to be associated with a particular person

Dog is an animal that can only be associated with the class Doctor (a subclass of Person)

Cat is an animal that can only be associated with the class Painter (a subclass of Person)

I need a method (named SomeMethod) that will take in new lists of animals

SomeMethod will primarily deal with the Owner property from the Animal class

using System.Collections.Generic;

namespace Test
{
    class Program
    {
        static void Main()
        {
            SomeMethod(new List<Animal<Person>>(), new List<Dog>());
        }

        public static void SomeMethod<T>(IList<Animal<T>> a, IList<Animal<T>> b)
            where T : Person
        {
            //do something
        }
    }

    class Animal<T>
        where T : Person
    {
        public T Owner { get; set; }
    }

    class Dog : Animal<Doctor>
    {

    }

    class Cat : Animal<Painter>
    {

    }

    class Person
    {

    }

    class Doctor : Person
    {

    }

    class Painter : Person
    {

    }

}

This code will not compile as SomeMethod() would not accept the 2 different lists as arguments. I will use the method to compare the "Owner" properties of each Animal. Would anybody have an idea?

5
  • Explain what you want the method to do - for instance, is it meant to filter the list of animals according to the type of person, and only assign if appropriate?
    – David M
    Commented Jan 20, 2010 at 8:50
  • 3
    You forgot a crucial detail: What is the problem?
    – Guffa
    Commented Jan 20, 2010 at 8:52
  • Tell your teacher that's a really crappy way to (ab)use generics.
    – Wim
    Commented Jan 20, 2010 at 9:23
  • I'm not really a student. The code above will be used in a real project. Of course, they aren't really dogs and cats I changed the class names. I'm not sure though that I made sensible representations as Dog and Cats don't have real relations with Doctors and Painters. In any case, did we go too far? Should we have refrained from using Generics altogether? I honestly want to know
    – user254703
    Commented Jan 20, 2010 at 9:36
  • Generics are a really good thing to use for strong typing, but only where they make sense. If your code doesn't require any casting and you feel like the compiler is helping you write better code then you probably are using it well. But like anything in OOP, just because you CAN do something doesn't mean you should.
    – Spence
    Commented Jan 20, 2010 at 11:05

1 Answer 1

1

What you have specified is a List<Dog> whereby the method signature requires a List<Animal<T>>. The method might want to add a Cat to your list of Dog. According to the methods signature this should legal, but your code would throw an exception as a Cat is not a Dog, and the List is specifically of Dog objects, not Animal<T>. Thus the C# compiler will refuse to compile this code.

So you have two choices:

  1. Specify that you have a List<Cat> and List<Dog> in mymethod.
  2. Create a IList<Animal<T>> and place Dog objects into it to pass to your method.
1
  • Can I add as an aside, if you don't specifically need list functions, you should pass IEnumerable functions. That way you can pass linq queries in and you can pass in ANY collection of animals, not just lists provided it can be enumerated.
    – Spence
    Commented Jan 20, 2010 at 9:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.