SortedList<TKey,TValue>.ContainsKey(TKey) SortedList<TKey,TValue>.ContainsKey(TKey) SortedList<TKey,TValue>.ContainsKey(TKey) SortedList<TKey,TValue>.ContainsKey(TKey) Method

定義

判斷 SortedList<TKey,TValue> 是否包含特定索引鍵。Determines whether the SortedList<TKey,TValue> contains a specific key.

public:
 virtual bool ContainsKey(TKey key);
public bool ContainsKey (TKey key);
abstract member ContainsKey : 'Key -> bool
override this.ContainsKey : 'Key -> bool
Public Function ContainsKey (key As TKey) As Boolean

參數

key
TKey TKey TKey TKey

要在 SortedList<TKey,TValue> 中尋找的索引鍵。The key to locate in the SortedList<TKey,TValue>.

傳回

如果 true 包含具有指定索引鍵的項目,則為 SortedList<TKey,TValue>,否則為 falsetrue if the SortedList<TKey,TValue> contains an element with the specified key; otherwise, false.

實作

例外狀況

範例

下列程式碼範例示範如何在ContainsKey Add呼叫方法之前, 使用方法來測試索引鍵是否存在。The following code example shows how to use the ContainsKey method to test whether a key exists prior to calling the Add method. 它也會示範如何使用TryGetValue方法來抓取值, 這是一種有效的方法, 可在程式經常嘗試不在排序清單中的索引鍵時, 取得值。It also shows how to use the TryGetValue method to retrieve values, which is an efficient way to retrieve values when a program frequently tries keys that are not in the sorted list. 最後, 它會使用Item[TKey]屬性 (中C#的索引子), 顯示最有效率的方法來測試金鑰是否存在。Finally, it shows the least efficient way to test whether keys exist, by using the Item[TKey] property (the indexer in C#).

這個程式碼範例是針對SortedList<TKey,TValue>類別提供之較大範例的一部分。This code example is part of a larger example provided for the SortedList<TKey,TValue> class.

// ContainsKey can be used to test keys before inserting
// them.
if (!openWith->ContainsKey("ht"))
{
    openWith->Add("ht", "hypertrm.exe");
    Console::WriteLine("Value added for key = \"ht\": {0}",
        openWith["ht"]);
}
// ContainsKey can be used to test keys before inserting 
// them.
if (!openWith.ContainsKey("ht"))
{
    openWith.Add("ht", "hypertrm.exe");
    Console.WriteLine("Value added for key = \"ht\": {0}", 
        openWith["ht"]);
}
' ContainsKey can be used to test keys before inserting 
' them.
If Not openWith.ContainsKey("ht") Then
    openWith.Add("ht", "hypertrm.exe")
    Console.WriteLine("Value added for key = ""ht"": {0}", _
        openWith("ht"))
End If
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
String^ value = "";
if (openWith->TryGetValue("tif", value))
{
    Console::WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console::WriteLine("Key = \"tif\" is not found.");
}
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient 
// way to retrieve values.
string value = "";
if (openWith.TryGetValue("tif", out value))
{
    Console.WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console.WriteLine("Key = \"tif\" is not found.");
}
' When a program often has to try keys that turn out not to
' be in the list, TryGetValue can be a more efficient 
' way to retrieve values.
Dim value As String = ""
If openWith.TryGetValue("tif", value) Then
    Console.WriteLine("For key = ""tif"", value = {0}.", value)
Else
    Console.WriteLine("Key = ""tif"" is not found.")
End If
// The indexer throws an exception if the requested key is
// not in the list.
try
{
    Console::WriteLine("For key = \"tif\", value = {0}.",
        openWith["tif"]);
}
catch (KeyNotFoundException^)
{
    Console::WriteLine("Key = \"tif\" is not found.");
}
// The indexer throws an exception if the requested key is
// not in the list.
try
{
    Console.WriteLine("For key = \"tif\", value = {0}.", 
        openWith["tif"]);
}
catch (KeyNotFoundException)
{
    Console.WriteLine("Key = \"tif\" is not found.");
}
' The default Item property throws an exception if the requested
' key is not in the list.
Try
    Console.WriteLine("For key = ""tif"", value = {0}.", _
        openWith("tif"))
Catch 
    Console.WriteLine("Key = ""tif"" is not found.")
End Try

備註

這個方法是 O (log n) 運算, 其中nCountThis method is an O(log n) operation, where n is Count.

適用於

另請參閱