473,242 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

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

Hello!

Below I have a working program.
I have one generic class called Farm<T>
with this header definition public class Farm<T: IEnumerable<Twhere 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<Tinstead 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<Tinherit from IEnumerable.

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

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

namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
Farm<Animalfarm = 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<CowdairyFarm = 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<Twhere T : Animal
{
private List<Tanimals = new List<T>();

public List<TAnimal
{
get { return animals; }
}

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

public IEnumerator<TGetEnumerator()
{
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<CowGetCows()
{
Farm<CowcowFarm = new Farm<Cow>();
foreach (T animal in animals)
if (animal is Cow)
cowFarm.animals.Add(animal as Cow);
return cowFarm;
}
}
}

//Tony
Jun 27 '08 #1
2 2461
Tony Johansson <jo*****************@telia.comwrote:
Below I have a working program.
I have one generic class called Farm<T>
with this header definition public class Farm<T: IEnumerable<Twhere 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<Tinstead 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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
On Jun 19, 12:05*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Tony Johansson <johansson.anders...@telia.comwrote:
Below I have a working program.
I have one generic class called Farm<T>
with this header definition public class Farm<T: IEnumerable<TwhereT :
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<Tinstead 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<Twill be able to work with your object (and also any
method that takes a plain IEnumerable, since, as you've noticed,
IEnumerable<Textends 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.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Doug Dew | last post by:
This won't compile: using IEnumerable<T> = System.Collections.Generic.IEnumerable<T>; namespace MyNamespace { public class MyClass<T> : IEnumerable<T> { // Appropriate stuff here }
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
2
by: Morgan Cheng | last post by:
In .Net 2.0, Generics are introduced. I found that IEnumerable<T> inherites from IEnumerable. This makes implementation of IEnumerable<Thas to have two GetEnumerator methods defined( one for...
1
by: Ezmeralda | last post by:
Hello, I need to an TCP/IP Interface for communication with an embedded device. Since I have two different design ideas in mind, I am wondering whether you could give me some hints to decide:...
1
by: Ezmeralda | last post by:
Hello, I need to an TCP/IP Interface for communication with an embedded device. Since I have two different design ideas in mind, I am wondering whether you could give me some hints to decide:...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
0
by: Marc Gravell | last post by:
You could bypass the List<T>'s tendency to use ICollection<Tby simply not giving it an ICollection<T- for example, with the C# 3 extension method below you could use: List<Tlist = new...
2
by: Berryl Hesh | last post by:
Converting in the other direction , IEnumerable<Interfaceto List<ImplInterface>, I can just create a new List<ImplInterface(data) where data is IEnumerable<Interface>. How can I convert the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.