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; }
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.

属性值

Object

如果找到 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[] 属性通过指定以下语法来访问集合中的特定元素: myCollection[key]You can use the Item[] 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[] 属性会覆盖旧值。However, if the specified key already exists in the SortedList, setting the Item[] 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 键进行排序,无论是在 IComparer 创建时指定的特定实现 SortedList 还是根据 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[] 实现为默认属性,该属性提供相同的索引功能。Visual Basic implements Item[] as a default property, which provides the same indexing functionality.

检索此属性的值是一个 O(log n) 操作,其中 nCountRetrieving the value of this property is an O(log n) operation, where n is Count. 如果键已存在于中,则设置属性是一个 O(log n) 操作 SortedListSetting 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).

适用于

另请参阅