SortedList.Item[Object] 属性

定义

获取或设置与 SortedList 对象中的特定键相关联的值。Gets or sets the value associated with a specific key in a SortedList object.

public:
 virtual property System::Object ^ default[System::Object ^] { System::Object ^ get(System::Object ^ key); void set(System::Object ^ key, System::Object ^ value); };
public virtual object this[object key] { get; set; }
member this.Item(obj) : obj with get, set
Default Public Overridable Property Item(key As Object) As Object

参数

key
Object

与要获取或设置的值相关联的键。The key associated with the value to get or set.

属性值

如果找到 key,则为与 SortedList 对象中的 key 参数相关联的值;否则为 nullThe value associated with the key parameter in the SortedList object, if key is found; otherwise, null.

实现

异常

keynullkey is null.

设置该属性,而且 SortedList 对象为只读。The property is set and the SortedList object is read-only.

- 或 --or- 设置了该属性,但 key 不存在于集合中,且 SortedList 具有固定的大小。The property is set, key does not exist in the collection, and the SortedList has a fixed size.

没有足够的可用内存来将元素添加到 SortedListThere is not enough available memory to add the element to the SortedList.

比较器引发异常。The comparer throws an exception.

注解

通过指定以下语法,可以使用 Item[Object] 属性访问集合中的特定元素: myCollection[key]You can use the Item[Object] property to access a specific element in a collection by specifying the following syntax: myCollection[key].

你还可以使用此属性来添加新元素,方法是设置 SortedList 对象中不存在的键的值(例如 myCollection["myNonexistentKey"] = myValue)You can also use this property to add new elements by setting the value of a key that does not exist in the SortedList object (for example, myCollection["myNonexistentKey"] = myValue). 但是,如果 SortedList中已经存在指定的键,则设置 Item[Object] 属性将覆盖旧值。However, if the specified key already exists in the SortedList, setting the Item[Object] property overwrites the old value. 与此相反,Add 方法不会修改现有元素。In contrast, the Add method does not modify existing elements.

不能 null键,但值可以为。A key cannot be null, but a value can be. 若要区分由于找不到指定的键而返回的 null,以及由于指定键的值 null而返回 null,请使用 Contains 方法或 ContainsKey 方法来确定列表中是否存在该键。To distinguish between null that is returned because the specified key is not found and null that is returned because the value of the specified key is null, use the Contains method or the ContainsKey method to determine if the key exists in the list.

SortedList 的元素按键排序,具体取决于创建 SortedList 时指定的特定 IComparer 实现,或根据键本身所提供的 IComparable 实现进行排序。The elements of a SortedList are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created or according to the IComparable implementation provided by the keys themselves.

C#语言使用this关键字来定义索引器,而不是实现 Keys 属性。The C# language uses the this keyword to define the indexers instead of implementing the Keys property. Visual Basic 将 Item[Object] 实现为默认属性,该属性提供相同的索引功能。Visual Basic implements Item[Object] as a default property, which provides the same indexing functionality.

检索此属性的值是一个 O(log n) 操作,其中 nCount的。Retrieving the value of this property is an O(log n) operation, where n is Count. 如果键已在 SortedList中,则设置属性是 O(log n) 操作。Setting the property is an O(log n) operation if the key is already in the SortedList. 如果键不在列表中,则为未排序的数据设置属性是 O(n) 操作,或者,如果新元素添加到列表的末尾,则为 O(log n)If the key is not in the list, setting the property is an O(n) operation for unsorted data, or O(log n) if the new element is added at the end of the list. 如果插入操作导致大小调整,则操作 O(n)If insertion causes a resize, the operation is O(n).

适用于

另请参阅