interface question

M

Mike P

I am looking through some code and have found a lot of methods that
return an interface such as ICollection.

What is the reason for this? If you return ICollection, does that mean
that you can return any collection object that inherits from it rather
than if you have a method returning an ArrayList, you are stuck with
only being able to return an ArrayList?
 
S

Sascha Bajonczak

Hi the reason is that are you Typesafe.

an ICollection defines only a Collection i think HashTable implements this
Interface, but the ArrayList implements the IList interface not the
ICollection,

so when you have a
ICollection GetCollection(params....) you get only a collection and not a
List type
 
J

Jon Skeet [C# MVP]

I am looking through some code and have found a lot of methods that
return an interface such as ICollection.  

What is the reason for this?  If you return ICollection, does that mean
that you can return any collection object that inherits from it rather
than if you have a method returning an ArrayList, you are stuck with
only being able to return an ArrayList?

Exactly. It's often very handy not to be tied to a particular
implementation. It means that at a later date you can choose which
implementation you use - you may even use different implementations
based on the contents. (For example, it's quite common to use a
singleton for immutable empty collections.)

Jon
 
J

Jon Skeet [C# MVP]

Hi the reason is that are you Typesafe.

Not really. You'd still be type safe if you declared the method to
return the concrete type. However, you'd have a lot less flexibility
of implementation later on.
an ICollection defines only a Collection i think HashTable implements this
Interface, but the ArrayList implements the IList interface not the
ICollection,

IList inherits from ICollection, so ArrayList has to implement
ICollection as well, effectively.

Jon
 
M

Mike P

So if I wanted to write a method that can return any object that
inherits from a particular interface, how would I find a listing of all
objects that inherit from that interface?

I could use the 'is' keyword to check an individual object in code :

ArrayList arr = new ArrayList();

bool bln = arr is ICollection;

But is there a quicker way?
 
J

Jon Skeet [C# MVP]

So if I wanted to write a method that can return any object that
inherits from a particular interface, how would I find a listing of all
objects that inherit from that interface?

I could use the 'is' keyword to check an individual object in code :

ArrayList arr = new ArrayList();

bool bln = arr is ICollection;

But is there a quicker way?

If you look at the interface in MSDN, you can usually find the classes
which implement it.

It's usually not an issue though - work out how you want to implement
the method, and you should find that the type you naturally want to
use implements an appropriate interface. At least, that's my
experience - I rarely have to pick around to try to find a list of
potential implementations.

Jon
 
M

Marc Gravell

how would I find a listing of all
objects that inherit from that interface?

I agree with Jon that normally you find that the "natural fit" already
meets the interface you want - but for info, note that "Reflector"
offers this ability; but beware that *lots* of things implement IList,
so *in this case* it isn't especially useful. But this can be useful
with more esoteric interfaces.

Note also, however, that it isn't uncommon for all the concrete
implementations of a specific interface to be internal to an assembly;
sometimes, the expectation is that you will provide your own
implementation of an interface.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top