Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an ICollection? [closed]

Tags:

c#

.net

What is a ICollection in C#?

private void SendEmail(string host, int port,
        string username, string password,
        string from, string to,
        string subject, string body,
        ICollection<string> attachedFiles)
like image 382
user1443110 Avatar asked Jun 07 '12 19:06

user1443110


4 Answers

The ICollection<T> interface is the base interface for classes in the System.Collections.Generic namespace.

The ICollection<T> interface extends IEnumerable<T> and is extended by IDictionary<TKey, TValue> and IList<T>.

An IDictionary<TKey, TValue> implementation is a collection of key/value pairs, like the Dictionary<TKey, TValue> class.

An IList<T> implementation is a collection of values, and its members can be accessed by index, like the List<T> class.

http://msdn.microsoft.com/en-us/library/92t2ye13.aspx

like image 72
Christopher Rathermel Avatar answered Sep 21 '22 16:09

Christopher Rathermel


I think what the OP is actually trying to understand, is what implements ICollection<string>, as obviously new ICollection<string>() isn't going to fly.

As Micah pointed out, List<string> implements ICollection<string>. If you want to know what else does, take a look at ILSpy, and find the ICollection<T> type, analyze it, and see what implements it. Here are some of my results

  • ArraySegment
  • List
  • LisT.SynchronizedList
  • LinkedList
  • SortedList
  • SortedSet

... and more

Also, a plain ol' string array also implements ICollection<string>

like image 26
payo Avatar answered Sep 22 '22 16:09

payo


ICollection is a interface that represents a collection, it also contains strongly typed members

Here is an example of how to implement that interface

http://support.microsoft.com/kb/307484

The Generic List Implements this interface

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

like image 37
Micah Armantrout Avatar answered Sep 21 '22 16:09

Micah Armantrout


A collection is a group of objects that have the same type (string in your example).

From the documentation:

Objects of any type can be grouped into a single collection of the type Object to take advantage of constructs that are inherent in the language. For example, the C# foreach statement (for each in Visual Basic) expects all objects in the collection to be of a single type.

ICollection is the interface definition for a collection.

like image 34
Martin Avatar answered Sep 22 '22 16:09

Martin