Re: Implementing an interface with qualified templates



Dave,

Why not just declare the interface as generic?

public class Animal
{}

public class Fish : Animal
{}

public interface IZoo<T> where T : Animal
{
List<T> Animals { get; }
void Feed(T a);
}

public Aquarium : IZoo<Fish>
{

}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Dave Booker" <dbooksta@xxxxxxxxxxxxxxxx> wrote in message
news:0C1D6E11-3CF6-489A-B5A2-56A4518C53DF@xxxxxxxxxxxxxxxx
I want to do something like this:

public class Animal;

public interface IZoo
{
List<Animal> Animals { get; }
void Feed(Animal a);
}

public Aquarium<FISH> : IZoo where FISH : Animal
{
public List<FISH> Animals { get ...;}
public void Feed(FISH a) {...}
}

It seems like this should be possible, because FISH are guaranteed to be
Animals. I can dodge the generic collection problem by substituting an
ArrayList for the List<>, but I don't see why I have to. And then there's
the problem of the function argument.

Is there any way to implement a (non-templated) interface with a qualified
template, like what I'm suggesting here?


.



Relevant Pages

  • compile error problems
    ... public class Chicken: Animal ... public override void MakeANoise() ... public class SuperCow: Cow ... foreach (T animal in animals) ...
    (microsoft.public.dotnet.languages.csharp)
  • XML Serialize / Deserialize Abstract Types
    ... Now I want to serialize BarnYardStall to XML, ... may add new types of derived Animals such as Cat and Cow. ... public class Horse: Animal ... public Animal Occupant ...
    (microsoft.public.dotnet.framework)
  • cant call super from method but I need to??
    ... how can I call animals growl while within a method of my ... public void growl() { ... public class dog extends animal { ... IDefaultCall default = new IDefaultCall { ...
    (comp.lang.java.programmer)
  • Re: Generic inheritance
    ... public class Chicken: Animal ... The fact that the Animal class is abstract doesn't disqualify it from being used as T in the Farm class. ... If you create some Cow instances and some Chicken instances and put in the Farm, and then loop through the contents of the farm, you will get Animal references: ... In the Farmclass you can do anything to the animals that is defined in the Animal class. ...
    (microsoft.public.dotnet.languages.csharp)