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)
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)
ICollection<T>
接口是 System.Collections.Generic
命名空间中类的基接口。
ICollection<T>
接口扩展了 IEnumerable<T>
接口,并被 IDictionary<TKey, TValue>
和 IList<T>
所扩展。
IDictionary<TKey, TValue>
接口的实现是键/值对的集合,就像 Dictionary<TKey, TValue>
类一样。
IList<T>
接口的实现是值的集合,其成员可以通过索引访问,就像 List<T>
类一样。
ICollection<string>
,因为显然new ICollection<string>()
不可行。List<string>
实现了ICollection<string>
。如果你想知道还有哪些实现了它,请查看ILSpy,并找到ICollection<T>
类型,分析它,看看谁实现了它。以下是我的一些结果:
string
数组也实现了ICollection<string>
。集合是指具有相同类型(例如,在您的示例中为字符串)的一组对象。
从文档中可以看到:
任何类型的对象都可以分组成单个Object类型的集合,以利用语言中固有的结构。例如,C# foreach语句(在Visual Basic中的for each)期望集合中的所有对象都是同一类型。
ICollection是集合的接口定义。