C# 什么是 ICollection?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10938955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:50:09  来源:igfitidea点击:

What is an ICollection?

c#.net

提问by user1443110

What is a ICollection in C#?

什么是 C# 中的 ICollection?

private void SendEmail(string host, int port,
        string username, string password,
        string from, string to,
        string subject, string body,
        ICollection<string> attachedFiles)

回答by Christopher Rathermel

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

ICollection<T>接口是用于类的基接口System.Collections.Generic命名空间。

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

所述ICollection<T>界面延伸IEnumerable<T>,并且通过扩展IDictionary<TKey, TValue>IList<T>

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

IDictionary<TKey, TValue>实现是键/值对的集合,如Dictionary<TKey, TValue>类。

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

一个IList<T>实现是一个值的集合,它的成员可以通过索引访问,就像List<T>类一样。

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

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

回答by Micah Armantrout

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

ICollection 是一个表示集合的接口,它还包含强类型成员

Here is an example of how to implement that interface

这是如何实现该接口的示例

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

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

The Generic List Implements this interface

通用列表实现了这个接口

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

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

回答by Martin

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.

可以将任何类型的对象分组到 Object 类型的单个集合中,以利用语言中固有的构造。例如,C# foreach 语句(在 Visual Basic 中为 each)期望集合中的所有对象都属于单一类型。

ICollectionis the interface definition for a collection.

ICollection是集合的接口定义。

回答by payo

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.

我认为 OP 实际上试图理解的是什么实现了ICollection<string>,因为显然new ICollection<string>()不会飞。

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

正如 Micah 指出的那样,List<string>实现ICollection<string>. 如果你想知道还有什么作用,看看ILSpy,找到ICollection<T>类型,分析它,看看是什么实现了它。这是我的一些结果

  • ArraySegment
  • List
  • LisT.SynchronizedList
  • LinkedList
  • SortedList
  • SortedSet
  • 数组段
  • 列表
  • ListT.SynchronizedList
  • 链表
  • 排序列表
  • 有序集

... and more

... 和更多

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

此外,一个普通的 ol'string数组也实现了ICollection<string>