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???
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>.
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>.
public
static
IList<AvatarImageInfo> GetAvatarImageSubsetSorted(string sortExpression,
int startRowIndex,
int maximumRows)
{
IList<AvatarImageInfo> avatarImages =
new List<AvatarImageInfo>();
//Name of Stored Procedure.
string sqlCommand =
"syl_AvatarImageGetSubsetSorted";
//Creates a SQL statement or stored procedure to execute against a data source.
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
//Create a parameter.
db.AddInParameter(dbCommand,
"sortExpression",
DbType.String, sortExpression);
db.AddInParameter(dbCommand,
"startRowIndex",
DbType.Int32, startRowIndex);
db.AddInParameter(dbCommand,
"maximumRows",
DbType.Int32, maximumRows);
//Invoke a SQL command.
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
AvatarImageInfo avatarImage =
null;
while (dataReader !=
null && dataReader.Read())
{
avatarImage =
new
AvatarImageInfo(dataReader);
//Add an avatarImage to the avatarImages collection.
avatarImages.Add(avatarImage);
}
}
//Return a generic list of AvatarImages.
return avatarImages;
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.
Member
97 Points
441 Posts
IList<> or ICollection<> which one is more efficient (better)?????
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
public static Collection<AvatarImageInfo> GetAvatarImages()
{
string key = AVATARIMAGE_KEY + "GetAll";
Collection<AvatarImageInfo> avatarImages = new Collection<AvatarImageInfo>();
if (avatarImageCache.Contains(key))
{
//Get an AvatarImageInfo collection from the cache.
avatarImages = (Collection<AvatarImageInfo>)avatarImageCache.GetData(key);
}
else
{
//Invoke a SQL command.
using (IDataReader dataReader = db.ExecuteReader(CommandType.StoredProcedure, "syl_AvatarImagesGetAll"))
{
AvatarImageInfo avatarImage = null;
while (dataReader != null && dataReader.Read())
{
avatarImage = new AvatarImageInfo(dataReader);
//Add an avatarImage to the avatarImages collection.
avatarImages.Add(avatarImage);
}
//Add a collection of AvatarImages to the cache.
avatarImageCache.Add(key, avatarImages);
}
}
//Return a generic collection of AvatarImages.
return avatarImages;
}
Participant
1530 Points
794 Posts
Re: IList<> or ICollection<> which one is more efficient (better)?????
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.
Member
1 Points
18 Posts
Re: IList<> or ICollection<> which one is more efficient (better)?????
Jan 12, 2007 08:08 AM|orjiani|LINK
Okay,
Icollection actually has limitation if you plan on indexing manually, try Ilist for auto indexing.
Ciao
Member
100 Points
33 Posts
Re: IList<> or ICollection<> which one is more efficient (better)?????
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.
Member
97 Points
441 Posts
Re: IList<> or ICollection<> which one is more efficient (better)?????
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>.
public
static IList<AvatarImageInfo> GetAvatarImageSubsetSorted(string sortExpression, int startRowIndex, int maximumRows){
IList<AvatarImageInfo> avatarImages = new List<AvatarImageInfo>(); //Name of Stored Procedure. string sqlCommand = "syl_AvatarImageGetSubsetSorted"; //Creates a SQL statement or stored procedure to execute against a data source. DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand); //Create a parameter.db.AddInParameter(dbCommand,
"sortExpression", DbType.String, sortExpression);db.AddInParameter(dbCommand,
"startRowIndex", DbType.Int32, startRowIndex);db.AddInParameter(dbCommand,
"maximumRows", DbType.Int32, maximumRows); //Invoke a SQL command. using (IDataReader dataReader = db.ExecuteReader(dbCommand)){
AvatarImageInfo avatarImage = null; while (dataReader != null && dataReader.Read()){
avatarImage =
new AvatarImageInfo(dataReader); //Add an avatarImage to the avatarImages collection.avatarImages.Add(avatarImage);
}
}
//Return a generic list of AvatarImages. return avatarImages;}
Member
100 Points
33 Posts
Re: IList<> or ICollection<> which one is more efficient (better)?????
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.
Member
97 Points
441 Posts
Re: IList<> or ICollection<> which one is more efficient (better)?????
Jan 21, 2007 09:57 PM|newbie06|LINK
Here is some more very good information about List<T> and Collection<T>
http://blogs.msdn.com/kcwalina/archive/2005/09/23/Collections.aspx