IDictionary.Item 属性
获取或设置具有指定键的元素。
**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Default Property Item ( _
key As Object _
) As Object
用法
Dim instance As IDictionary
Dim key As Object
Dim value As Object
value = instance(key)
instance(key) = value
Object this [
Object key
] { get; set; }
property Object^ default [Object^] {
Object^ get (Object^ key);
void set (Object^ key, Object^ value);
}
/** @property */
Object get_Item (Object key)
/** @property */
void set_Item (Object key, Object value)
JScript 支持使用索引属性,但不支持进行新的声明。
参数
- key
要获取或设置的元素的键。
属性值
带有指定键的元素。
异常
异常类型 | 条件 |
---|---|
key 为 空引用(在 Visual Basic 中为 Nothing)。 |
|
设置该属性,而且 IDictionary 对象为只读。 - 或 - 设置该属性,集合中不存在 key,而且 IDictionary 具有固定大小。 |
备注
通过使用下面的语法,此属性提供访问集合中特定元素的能力:myCollection[key]
。
通过设置字典中不存在的键值(例如,myCollection["myNonexistentKey"] = myValue
),还可以使用 Item 属性添加新元素。但是,如果字典中已存在指定的键,则设置 Item 属性将改写旧值。相比之下,Add 方法不修改现有元素。
实现在是否允许键为 空引用(在 Visual Basic 中为 Nothing) 方面有所不同。
示例
下面的代码示例演示如何实现 Item 属性。此代码示例是为 IDictionary 类提供的一个更大示例的一部分。
Public Property Item(ByVal key As Object) As Object Implements IDictionary.Item
Get
' If this key is in the dictionary, return its value.
Dim index As Integer
If TryGetIndexOfKey(key, index) Then
' The key was found return its value.
Return items(index).Value
Else
' The key was not found return null.
Return Nothing
End If
End Get
Set(ByVal value As Object)
' If this key is in the dictionary, change its value.
Dim index As Integer
If TryGetIndexOfKey(key, index) Then
' The key was found change its value.
items(index).Value = value
Else
' This key is not in the dictionary add this key/value pair.
Add(key, value)
End If
End Set
End Property
public object this[object key]
{
get
{
// If this key is in the dictionary, return its value.
Int32 index;
if (TryGetIndexOfKey(key, out index))
{
// The key was found; return its value.
return items[index].Value;
}
else
{
// The key was not found; return null.
return null;
}
}
set
{
// If this key is in the dictionary, change its value.
Int32 index;
if (TryGetIndexOfKey(key, out index))
{
// The key was found; change its value.
items[index].Value = value;
}
else
{
// This key is not in the dictionary; add this key/value pair.
Add(key, value);
}
}
}
public:
virtual property Object^ default[Object^]
{
Object^ get(Object^ key)
{
// If this key is in the dictionary, return its value.
int index;
if (TryGetIndexOfKey(key, &index))
{
// The key was found; return its value.
return items[index]->Value;
}
else
{
// The key was not found; return null.
return nullptr;
}
}
void set(Object^ key, Object^ value)
{
// If this key is in the dictionary, change its value.
int index;
if (TryGetIndexOfKey(key, &index))
{
// The key was found; change its value.
items[index]->Value = value;
}
else
{
// This key is not in the dictionary; add this
// key/value pair.
Add(key, value);
}
}
}
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0