Hello!
Below I have a working program.
I have one generic class called Farm<T>
with this header definition public class Farm<T: IEnumerable<Twh ere 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<Tin stead 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<Tin herit from IEnumerable.
So as a summary I can't see what purpose or advantage I get when I
implement IEnumerable<Tin stead of
IEnumerable.?
using System;
using System.Collecti ons.Generic;
using System.Collecti ons;
using System.Text;
namespace ConsoleApplicat ion9
{
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<CowdairyFa rm = farm.GetCows();
dairyFarm.FeedT heAnimals();
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.WriteLi ne("{0} says 'cluck!'", Name);
}
public override void Feed()
{
Console.WriteLi ne(Name + " has been feed(Chicken)") ;
}
}
public class Cow : Animal
{
public Cow(string name) : base(name) {}
public override void MakeANoise()
{
Console.WriteLi ne("{0} says 'moo!'", Name);
}
public override void Feed()
{
Console.WriteLi ne(Name + " has been fed(Cow)");
}
}
public class SuperCow : Cow
{
public SuperCow(string name) : base(name)
{ }
public void Fly()
{
Console.WriteLi ne("{0} is flying!", Name);
}
public override void MakeANoise()
{
Console.WriteLi ne("{0} says 'here I come to save the day!'",
Name);
}
public override void Feed()
{
Console.WriteLi ne(Name + " has been feed(SuperCow)" );
}
}
public class Farm<T: IEnumerable<Twh ere T : Animal
{
private List<Tanimals = new List<T>();
public List<TAnimal
{
get { return animals; }
}
IEnumerator IEnumerable.Get Enumerator()
{
return animals.GetEnum erator();
}
public IEnumerator<TGe tEnumerator()
{
return animals.GetEnum erator();
}
public void MakeNoises()
{
foreach (T animal in animals)
animal.MakeANoi se();
}
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
Below I have a working program.
I have one generic class called Farm<T>
with this header definition public class Farm<T: IEnumerable<Twh ere 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<Tin stead 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<Tin herit from IEnumerable.
So as a summary I can't see what purpose or advantage I get when I
implement IEnumerable<Tin stead of
IEnumerable.?
using System;
using System.Collecti ons.Generic;
using System.Collecti ons;
using System.Text;
namespace ConsoleApplicat ion9
{
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<CowdairyFa rm = farm.GetCows();
dairyFarm.FeedT heAnimals();
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.WriteLi ne("{0} says 'cluck!'", Name);
}
public override void Feed()
{
Console.WriteLi ne(Name + " has been feed(Chicken)") ;
}
}
public class Cow : Animal
{
public Cow(string name) : base(name) {}
public override void MakeANoise()
{
Console.WriteLi ne("{0} says 'moo!'", Name);
}
public override void Feed()
{
Console.WriteLi ne(Name + " has been fed(Cow)");
}
}
public class SuperCow : Cow
{
public SuperCow(string name) : base(name)
{ }
public void Fly()
{
Console.WriteLi ne("{0} is flying!", Name);
}
public override void MakeANoise()
{
Console.WriteLi ne("{0} says 'here I come to save the day!'",
Name);
}
public override void Feed()
{
Console.WriteLi ne(Name + " has been feed(SuperCow)" );
}
}
public class Farm<T: IEnumerable<Twh ere T : Animal
{
private List<Tanimals = new List<T>();
public List<TAnimal
{
get { return animals; }
}
IEnumerator IEnumerable.Get Enumerator()
{
return animals.GetEnum erator();
}
public IEnumerator<TGe tEnumerator()
{
return animals.GetEnum erator();
}
public void MakeNoises()
{
foreach (T animal in animals)
animal.MakeANoi se();
}
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
Comment