PC Review


Reply
Thread Tools Rate Thread

What is the disadvantage if I don't implement IEnumerable<T>

 
 
Tony Johansson
Guest
Posts: n/a
 
      18th Jun 2008
Hello!

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.

So what advantage do I get if I implement the generic interface
IEnumerable<T> instead of the
generell IEnumerable ?
Can you also give me some example that show on advantage to implement the
generic interface IEnumerable<T>?

I know that this IEnumerable<T> inherit from IEnumerable.

So as a summary I can't see what purpose or advantage I get when I
implement IEnumerable<T> instead of
IEnumerable.?

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
Farm<Animal> farm = new Farm<Animal>();
farm.Animal.Add(new Cow("Jack"));
farm.Animal.Add(new Chicken("Vera"));
farm.Animal.Add(new Chicken("Sally"));
farm.Animal.Add(new SuperCow("Kevin"));

farm.MakeNoises();
Farm<Cow> dairyFarm = farm.GetCows();
dairyFarm.FeedTheAnimals();

foreach (Cow cow in dairyFarm)
{
if (cow is SuperCow)
((SuperCow)cow).Fly();
}
Console.ReadKey();

}
}




public abstract class Animal
{
string name;
public Animal(string name)
{
this.name = name;
}

public string Name
{
get { return name; }
}

public abstract void MakeANoise();
public abstract void Feed();
}

public class Chicken : Animal
{
public Chicken(string name) : base(name) {}

public override void MakeANoise()
{
Console.WriteLine("{0} says 'cluck!'", Name);
}

public override void Feed()
{
Console.WriteLine(Name + " has been feed(Chicken)");
}
}

public class Cow : Animal
{
public Cow(string name) : base(name) {}

public override void MakeANoise()
{
Console.WriteLine("{0} says 'moo!'", Name);
}

public override void Feed()
{
Console.WriteLine(Name + " has been fed(Cow)");
}
}

public class SuperCow : Cow
{
public SuperCow(string name) : base(name)
{ }

public void Fly()
{
Console.WriteLine("{0} is flying!", Name);
}

public override void MakeANoise()
{
Console.WriteLine("{0} says 'here I come to save the day!'",
Name);
}

public override void Feed()
{
Console.WriteLine(Name + " has been feed(SuperCow)");
}
}

public class Farm<T> : IEnumerable<T> where T : Animal
{
private List<T> animals = new List<T>();

public List<T> Animal
{
get { return animals; }
}

IEnumerator IEnumerable.GetEnumerator()
{
return animals.GetEnumerator();
}

public IEnumerator<T> GetEnumerator()
{
return animals.GetEnumerator();
}


public void MakeNoises()
{
foreach (T animal in animals)
animal.MakeANoise();
}

public void FeedTheAnimals()
{
foreach (T animal in animals)
animal.Feed();
}

public Farm<Cow> GetCows()
{
Farm<Cow> cowFarm = new Farm<Cow>();
foreach (T animal in animals)
if (animal is Cow)
cowFarm.animals.Add(animal as Cow);
return cowFarm;
}
}
}

//Tony


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      18th Jun 2008
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
 
Reply With Quote
 
Pavel Minaev
Guest
Posts: n/a
 
      19th Jun 2008
On Jun 19, 12:05*am, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> Tony Johansson <johansson.anders...@telia.com> 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> whereT :
> > 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.


In addition to all this, there is one more practical reason to
implement IEnumerable<T>: if you do that, then any method that takes
IEnumerable<T> will be able to work with your object (and also any
method that takes a plain IEnumerable, since, as you've noticed,
IEnumerable<T> extends IEnumerable). For example, the vast majority of
LINQ methods require IEnumerable<T>. You can use
Enumerable.Cast<TResult>() method to create wrappers as needed, of
course, but, same as with plain casts, it gets tedious to write it
again and again, and there is an associated performance hit as well.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implement IEnumerable(Of T) and Inherit from CollectionBase Paul Linville Microsoft Dot NET Framework 5 10th Jul 2008 01:00 PM
Implement Interface IEnumerator and IEnumerable Tony Johansson Microsoft C# .NET 2 28th Feb 2008 10:22 PM
Reflection to detemine collection or implement of IEnumerable cwertman@gmail.com Microsoft Dot NET 1 6th Dec 2006 08:45 PM
Won't compile: using IEnumerable<T> = System.Collections.Generic.IEnumerable<T>; Doug Dew Microsoft C# .NET 6 4th Aug 2004 09:49 AM
ArrayList of ArrayLists: How to implement IEnumerable =?Utf-8?B?UG9udGlNYXg=?= Microsoft ASP .NET 0 5th Jul 2004 03:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:40 PM.