OrderedDictionary.Item[] 属性

定义

获取或设置指定的值。

重载

名称 说明
Item[Int32]

获取或设置指定索引处的值。

Item[Object]

获取或设置具有指定键的值。

Item[Int32]

Source:
OrderedDictionary.cs
Source:
OrderedDictionary.cs
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, set
Default Public Property Item(index As Integer) As Object

参数

index
Int32

要获取或设置的值的从零开始的索引。

属性值

指定索引处的项的值。

实现

例外

正在设置该属性, OrderedDictionary 并且集合为只读。

index 小于零。

-或-

index 等于或大于 Count

注解

此属性允许使用以下语法访问集合中的特定元素: myCollection[index]

C# 语言 使用此关键字来 定义索引器,而不是实现 Item[] 属性。 Visual Basic实现 Item[] 作为 default 属性,它提供相同的索引功能。

适用于

Item[Object]

Source:
OrderedDictionary.cs
Source:
OrderedDictionary.cs
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, set
Default 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 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

适用于