OrderedDictionary.Item[] Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Recebe ou define o valor especificado.
Sobrecargas
| Name | Description |
|---|---|
| Item[Int32] |
Obtém ou define o valor no índice especificado. |
| Item[Object] |
Recebe ou define o valor com a chave especificada. |
Item[Int32]
- Origem:
- OrderedDictionary.cs
- Origem:
- OrderedDictionary.cs
- Origem:
- OrderedDictionary.cs
- Origem:
- OrderedDictionary.cs
- Origem:
- OrderedDictionary.cs
Obtém ou define o valor no índice especificado.
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
Parâmetros
- index
- Int32
O índice em base zero do valor a obter ou definir.
Valor de Propriedade
O valor do item no índice especificado.
Implementações
Exceções
A propriedade está a ser definida e a OrderedDictionary coleção é só de leitura.
Observações
Esta propriedade permite-lhe aceder a um elemento específico da coleção usando a seguinte sintaxe: myCollection[index].
A linguagem C# usa esta palavra-chave para definir os indexadores em vez de implementar a Item[] propriedade. Visual Basic implementa Item[] como uma propriedade default, que fornece a mesma funcionalidade de indexação.
Aplica-se a
Item[Object]
- Origem:
- OrderedDictionary.cs
- Origem:
- OrderedDictionary.cs
- Origem:
- OrderedDictionary.cs
- Origem:
- OrderedDictionary.cs
- Origem:
- OrderedDictionary.cs
Recebe ou define o valor com a chave especificada.
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
Parâmetros
- key
- Object
A chave do valor a obter ou definir.
Valor de Propriedade
O valor associado à chave especificada. Se a chave especificada não for encontrada, tentar obtê-la retorna null, e tentar defini-la cria um novo elemento usando a chave especificada.
Implementações
Exceções
A propriedade está a ser definida e a OrderedDictionary coleção é só de leitura.
Exemplos
O exemplo de código seguinte demonstra a modificação de uma OrderedDictionary coleção. Neste exemplo, a Item[] propriedade é usada para modificar a entrada do dicionário com a chave "testKey2". Este código faz parte de um exemplo de código maior que pode ser visto em 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
Observações
Esta propriedade permite-lhe aceder a um elemento específico da coleção usando a seguinte sintaxe: myCollection[key].
Também pode usar a Item[] propriedade para adicionar novos elementos definindo o valor de uma chave que não existe na OrderedDictionary coleção (por exemplo, myCollection["myNonexistentKey"] = myValue). No entanto, se a chave especificada já existir no OrderedDictionary, definindo a Item[] propriedade sobrescreve o valor antigo. Em contraste, o Add método não modifica elementos existentes.
Uma chave não pode ser null, mas um valor pode ser. Para distinguir entre null isso é devolvido porque a chave especificada não é encontrada e null este é devolvido porque o valor da chave especificada é null, use o Contains método para determinar se a chave existe no OrderedDictionary.