0

Suppose I have a class

Class Apple<T> where T: IComparable<int>{}

Now while creating an object for Apple I'll do it like

Apple<int> obj = new Apple<int>();

The above code will work.

If I replace the same code with any other Generic Interface like IComparer<>,IEnumerable<> etc.. object creation like the above will not work and we have to declare it like

Apple<IEnumerable<int>> obj = new Apple<IEnumerable<int>>();

Why is it like that? Why is it the way which i declared for IComparable<> is only working for it and not for any other interface?

Pintu Paul
  • 389
  • 3
  • 18
srikar kulkarni
  • 630
  • 1
  • 6
  • 16

1 Answers1

6

Nothing special about IComparable<T>, but about T itself, which - in case of int- also implements IComparable<int>. However int surely does not implement IEnumerable<int>.

Or in other words: you can of course compare every int with another int. However you can“t iterate an int.

Bakudan
  • 19,134
  • 9
  • 53
  • 73
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111