Hashtable.Item[Object] 属性

定义

获取或设置与指定的键关联的值。Gets or sets the value associated with the specified key.

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 whose value to get or set.

属性值

与指定的键相关联的值。The value associated with the specified key. 如果未找到指定的键,尝试获取它将返回 null,尝试设置它将使用指定的键创建新元素。If the specified key is not found, attempting to get it returns null, and attempting to set it creates a new element using the specified key.

实现

异常

keynullkey is null.

设置了该属性,但 Hashtable 为只读。The property is set and the Hashtable is read-only.

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

注解

可以使用下面的语法通过此属性访问集合中的特定元素:myCollection[key]This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[key].

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

键不能为 null,但值可以为。A key cannot be null, but a value can be. 若要区分由于找不到指定的键 @no__t,而返回的 null,因为指定键的值 @no__t 为-2,则使用 @no__t 的方法或 @no__t 的方法来确定列表中是否存在该键。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.

检索此属性的值是 O(1) 操作;设置属性也是一个 @no__t 的操作。Retrieving the value of this property is an O(1) operation; setting the property is also an O(1) operation.

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

适用于

另请参阅