OrderedDictionary.Item[] 属性  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置指定的值。
重载
| Item[Int32] | 获取或设置指定索引处的值。 | 
| Item[Object] | 获取或设置具有指定键的值。 | 
Item[Int32]
- Source:
- OrderedDictionary.cs
- Source:
- OrderedDictionary.cs
- Source:
- OrderedDictionary.cs
获取或设置指定索引处的值。
public:
 property System::Object ^ default[int] { System::Object ^ get(int index); void set(int index, System::Object ^ value); };public object this[int index] { get; set; }public object? this[int index] { get; set; }member this.Item(int) : obj with get, setDefault Public Property Item(index As Integer) As Object参数
- index
- Int32
要获取或设置的值的从零开始索引。
属性值
在指定索引处的项的值。
实现
例外
正在设置此属性,但 OrderedDictionary 集合是只读的。
注解
此属性允许使用以下语法访问集合中的特定元素: myCollection[index]。
C# 语言使用此关键字 (keyword) 来定义索引器,而不是实现 Item[] 属性。 Visual Basic 实现 Item[] 为 默认属性,该属性提供相同的索引功能。
适用于
Item[Object]
- Source:
- OrderedDictionary.cs
- Source:
- OrderedDictionary.cs
- Source:
- OrderedDictionary.cs
获取或设置具有指定键的值。
public:
 property System::Object ^ default[System::Object ^] { System::Object ^ get(System::Object ^ key); void set(System::Object ^ key, System::Object ^ value); };public object this[object key] { get; set; }public object? this[object key] { get; set; }member this.Item(obj) : obj with get, setDefault Public Property Item(key As Object) As Object参数
- key
- Object
要获取或设置的值的键。
属性值
与指定的键相关联的值。 如果未找到指定的键,尝试获取它将返回 null,尝试设置它将使用指定的键创建新元素。
实现
例外
正在设置此属性,但 OrderedDictionary 集合是只读的。
示例
下面的代码示例演示了对集合的 OrderedDictionary 修改。 在此示例中, Item[] 属性用于修改具有键 "testKey2"的字典条目。 此代码是可在 中查看的较大代码示例的一 OrderedDictionary部分。
// Modifying the OrderedDictionary
if (!myOrderedDictionary->IsReadOnly)
{
    // Insert a new key to the beginning of the OrderedDictionary
    myOrderedDictionary->Insert(0, "insertedKey1", "insertedValue1");
    // Modify the value of the entry with the key "testKey2"
    myOrderedDictionary["testKey2"] = "modifiedValue";
    // Remove the last entry from the OrderedDictionary: "testKey3"
    myOrderedDictionary->RemoveAt(myOrderedDictionary->Count - 1);
    // Remove the "keyToDelete" entry, if it exists
    if (myOrderedDictionary->Contains("keyToDelete"))
    {
        myOrderedDictionary->Remove("keyToDelete");
    }
}
// Modifying the OrderedDictionary
if (!myOrderedDictionary.IsReadOnly)
{
    // Insert a new key to the beginning of the OrderedDictionary
    myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1");
    // Modify the value of the entry with the key "testKey2"
    myOrderedDictionary["testKey2"] = "modifiedValue";
    // Remove the last entry from the OrderedDictionary: "testKey3"
    myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1);
    // Remove the "keyToDelete" entry, if it exists
    if (myOrderedDictionary.Contains("keyToDelete"))
    {
        myOrderedDictionary.Remove("keyToDelete");
    }
}
' Modifying the OrderedDictionary
If Not myOrderedDictionary.IsReadOnly Then
    ' Insert a new key to the beginning of the OrderedDictionary
    myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1")
    ' Modify the value of the entry with the key "testKey2"
    myOrderedDictionary("testKey2") = "modifiedValue"
    ' Remove the last entry from the OrderedDictionary: "testKey3"
    myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1)
    ' Remove the "keyToDelete" entry, if it exists
    If (myOrderedDictionary.Contains("keyToDelete")) Then
        myOrderedDictionary.Remove("keyToDelete")
    End If
End If
注解
此属性允许使用以下语法访问集合中的特定元素: myCollection[key]。
还可以使用 Item[] 属性通过设置集合中 OrderedDictionary 不存在的键的值来添加新元素, (例如, myCollection["myNonexistentKey"] = myValue) 。 但是,如果 指定的键已存在于 中 OrderedDictionary,设置 属性 Item[] 将覆盖旧值。 相反, Add 方法不修改现有元素。
键不能是 null,但值可以是 。 若要区分 null 由于找不到指定键而返回的 ,以及 null 由于指定键 null的值为 而返回的 ,请使用 Contains 方法确定中是否存在 OrderedDictionary该键。