Tony Johansson <(E-Mail Removed)> wrote:
> Below I have a working program.
> I have one generic class called Farm<T>
> with this header definition public class Farm<T> : IEnumerable<T> where T :
> Animal
>
> Now to my question I changed the inheritance of the IEnumerable from the
> generic IEnumerable<T>
> to the generel IEnumerable and the program function just the same so no
> difference occured.
In this particular program, yes.
> So what advantage do I get if I implement the generic interface
> IEnumerable<T> instead of the
> generell IEnumerable ?
You end up with a more strongly typed API. This enables things like
LINQ to Objects to work much more smoothly, and fewer casts which need
to be checked at execution time. (In fact your foreach loops ended up
needing execution time casts anyway, but that's often not the case.)
In general, when you *can* implement a generic interface instead of a
nongeneric one, it's a good idea to do so.
--
Jon Skeet - <(E-Mail Removed)>
Web site:
http://www.pobox.com/~skeet
Blog:
http://www.msmvps.com/jon.skeet
C# in Depth:
http://csharpindepth.com