Last post Jan 21, 2007 09:57 PM by newbie06
Jan 11, 2007 01:08 PM|newbie06|LINK
In my code I use ICollection<> for some reason I decided on ICollection<> after doing some research but I forgot why I am using ICollection<>. Look at different samples I
have come across on the web it seems that most people use IList<> which is better or does it matter???
Portion of my code using ICollection
static Collection<AvatarImageInfo> GetAvatarImages()
string key = AVATARIMAGE_KEY +
Collection<AvatarImageInfo> avatarImages =
//Get an AvatarImageInfo collection from the cache.
avatarImages = (Collection<AvatarImageInfo>)avatarImageCache.GetData(key);
//Invoke a SQL command.
using (IDataReader dataReader = db.ExecuteReader(CommandType.StoredProcedure,
AvatarImageInfo avatarImage =
while (dataReader !=
null && dataReader.Read())
avatarImage = new
//Add an avatarImage to the avatarImages collection.
//Add a collection of AvatarImages to the cache.
//Return a generic collection of AvatarImages.
Jan 11, 2007 05:25 PM|Kelsey|LINK
I could be wrong but I think using a List template gives you the ability to directly index an element.
Jan 12, 2007 08:08 AM|orjiani|LINK
Icollection actually has limitation if you plan on indexing manually, try Ilist for auto indexing.
Jan 13, 2007 06:02 PM|SvDeursen|LINK
Your question is about the ICollection<T> interface but in your code you use a Collection<T>
class and there is a difference. Like the others replied, IList<T> has the ability to directly index an element. IList<T> implement the ICollection<T> interface, so it is an ICollection<T> with some extra functionality.
However, the Collection<T> class not only implements the ICollection<T> interface, it also
implement the IList<T> interface. As a matter of fact, the Collection<T> and List<T> classes both implement the
exact same interfaces!
I explained just a couple of hours ago the difference between Collection<T> and List<T> on another thread. So without having to repeat myself... :-) here is the link to that thread:
Difference between List Of<T> and Collection Of<T>.
I hope this helps.
Jan 21, 2007 05:31 PM|newbie06|LINK
For a new guy this is all confusing. So would you say then that in my cause it would be best to keep the code with Collection<T> returnning Collection<T> or would it be better (faster) to use IList<T>??? Could you give me your opinion, here is my code again
this time using a IList<T> rather than a Collection<T>.
Jan 21, 2007 07:50 PM|SvDeursen|LINK
Of course it depends on what you want to do with your code. In your case there is probably never the need to extend the collection you are returning -because you just return a list of items from the db- so it doesn't really matter what you return. You may
as well return an array instead. And you can still create a Collection<AvatarImageInfo> and return it as IList<AvatarImageInfo> (because Collection<T> implements IList<T>).
From a performance perspective, declaring the avatarImages as List<AvatarImageInfo> instead of IList<AvatarImageInfo> gives you theoretically a better performance (because you'll have normal method calls (while filling the list) on avatorImages instead of
interface method calls). Also generally speaking a List<T> is less expensive than a Collection<T>, but there is probably not a noticeable difference, because your database query is likely to be much more expensive than filling your list. So with the current
contract of your method (the contract of returning a IList<AvatarImageInfo>) you can return a List<AvatarImageInfo>, Collection<AvatarImageInfo> or AvatarImageInfo array.
Also be ware that you can't compare a class with an interface, so you can't compare (the class) Collection<T> with (the interface) IList<T>, because an interface has no implementation (and therefore no performance characteristics). Besides Collection<T>
is a IList<T> so are you comparing it with itself?
But to answer your question, in your case it probably won't really matter what you return. While keeping the method return a IList<AvatarImageInfo> you are flexible to decide to return Collection<AvatarImageInfo> or AvatarImageInfo instead of a List<AvatarImageInfo>
later on. This way you won't have to change the method's contract which is a good thing.
I hope this helps.
Jan 21, 2007 09:57 PM|newbie06|LINK
Here is some more very good information about List<T> and Collection<T>