Loading ...

Collection vs ICollection

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Collection vs ICollection

Collection vs ICollection

Posts under the topic: Collection vs ICollection

Posted: 10/21/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

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.


Posted: 10/21/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

The ICollection interface is the base interface for classes in the System.Collections namespace.


Posted: 10/21/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Thank you so much Vinz for the quick reply.

Can you more explain:)


Posted: 10/21/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391
  Answered

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.

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

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Thank you Hajan.Nice replySmile


Page 1 of 1 (5 items)