Export (0) Print
Expand All

Dictionary.TryGetValue Method

Gets the value associated with the specified key.

Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)

public bool TryGetValue (
	TKey key,
	out TValue value
)
public final boolean TryGetValue (
	TKey key, 
	/** @attribute OutAttribute() */ /** @ref */ TValue value
)
Not applicable.

Parameters

key

The key of the value to get.

value

When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

Return Value

true if the Dictionary contains an element with the specified key; otherwise, false.

Exception typeCondition

ArgumentNullException

key is a null reference (Nothing in Visual Basic).

This method combines the functionality of the ContainsKey method and the Item property.

If the key is not found, then the value parameter gets the appropriate default value for the value type TValue; for example, 0 (zero) for integer types, false for Boolean types, and a null reference (Nothing in Visual Basic) for reference types.

Use the TryGetValue method if your code frequently attempts to access keys that are not in the dictionary. Using this method is more efficient than catching the KeyNotFoundException thrown by the Item property.

This method approaches an O(1) operation.

The example shows how to use the TryGetValue method as a more efficient way to retrieve values in a program that frequently tries keys that are not in the dictionary. For contrast, the example also shows how the Item property (the indexer in C#) throws exceptions when attempting to retrieve nonexistent keys.

This code example is part of a larger example provided for the Dictionary class.

    // When a program often has to try keys that turn out not to
    // be in the dictionary, 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.");
    }

...

    // The indexer throws an exception if the requested key is
    // not in the dictionary.
    try
    {
        Console.WriteLine("For key = \"tif\", value = {0}.", 
            openWith["tif"]);
    }
    catch (KeyNotFoundException)
    {
        Console.WriteLine("Key = \"tif\" is not found.");
    }

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft