Posted: 10/21/2010
I have created a dummy projet for testing collection and ICollection.I have a user class and wanted to create a collection.Example -
ICollection<User> users = new Collection<User>(); Collection<User> users = new Collection<User>();
Both line are working fine whether I use Collection or I collection.Now can anyone tell me what is difference between above two line?
Thanks in advance.
The ICollection interface is the base interface for classes in the System.Collections namespace.
Thank you so much Vinz for the quick reply.
Can you more explain:)
mohit said: user="mohit kumar"]I have created a dummy projet for testing collection and ICollection.I have a user class and wanted to create a collection.Example -ICollection<User> users = new Collection<User>(); Collection<User> users = new Collection<User>(); Both line are working fine whether I use Collection or I collection.Now can anyone tell me what is difference between above two line? Thanks in advance.
user="mohit kumar"]
If you know the difference between classes and interface, that is the first difference between ICollection from Collection.
They can do the same job (in your case) because the Collection class inherits from the ICollection interface.
[SerializableAttribute] [ComVisibleAttribute(false)] public class Collection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
It also inherits the IList, IEnumerable<T>, IList<T>, ICollection<T> which makes it very good to work against collections.
However, it's recommended to use the Collection class if you have some properties or methods which will return Collections. This is the most important case where we use the ObjectModel namespace and its Classes.
On the other hand, the ICollection interface derives only from IEnumerable interface.
[ComVisibleAttribute(true)] public interface ICollection : IEnumerable
So, you can see the difference.
Hope this helps.
Posted: 10/22/2010
Thank you Hajan.Nice reply