0

How do I define a generic class where I have 2 types that must be different?

// General breeding - could be any animal/species with any animal/species
public abstract class Breed<TAnimal1, TAnimal2>
    where TAnimal1 : Animal
    where TAnimal2 : Animal
{
}

// Must breed among same animal/species
public abstract class IntraBreed<TAnimal> : Breed<TAnimal, TAnimal>
    where TAnimal : Animal
{
}

// Must breed between 2 different animals/species
public abstract class CrossBreed<TAnimal1, TAnimal2> : Breed<TAnimal1, TAnimal2>
    where TAnimal1 : Animal
    where TAnimal2 : Animal
    where TAnimal1 != TAnimal2 // I need something like this
{
}
Jai
  • 8,165
  • 2
  • 21
  • 52
  • 1
    Does this answer your question https://stackoverflow.com/a/8057285/7299782 – Anu Viswan Feb 06 '20 at 07:45
  • There is no such generic constraint in the language. You'd have to enforce it at runtime. – Sweeper Feb 06 '20 at 07:45
  • @AnuViswan Thanks, that's something I didn't realize, and it's very helpful. Strange thing is, now I'm more worried about the `IntraBreed` more than `CrossBreed` :( – Jai Feb 06 '20 at 08:00

0 Answers0