1Dictionary.TryGetValue 方法 试用记 Mark if (name == null || name == "")
 2Dictionary.TryGetValue 方法 试用记 Mark                return null;
 3Dictionary.TryGetValue 方法 试用记 Mark            XLine obj;
 4Dictionary.TryGetValue 方法 试用记 Mark            if (xlines.TryGetValue(name, out obj))
 5            }
在盖次的MSDN里找到一些东东.
http://msdn.microsoft.com/zh-cn/library/zkw5c9ak(VS.80).aspx
.NET Framework 类库
Dictionary.TryGetValue 方法

注意:此方法在 .NET Framework 2.0 版中是新增的。

获取与指定的键相关联的值。

命名空间:System.Collections.Generic
程序集:mscorlib(在 mscorlib.dll 中)

Visual Basic(声明)
Function TryGetValue ( _
key As TKey, _
<OutAttribute> ByRef value As TValue _
) As Boolean
Visual Basic(用法)
Of TKey, TValue)
Dim key As TKey
Dim value As TValue
Dim returnValue As Boolean
returnValue = instance.TryGetValue(key, value)
C#
bool TryGetValue (
TKey key,
out TValue value
)
C++
public:
virtual bool TryGetValue (
TKey key,
[OutAttribute] TValue% value
) sealed
J#
public final boolean TryGetValue (
TKey key,
/** @attribute OutAttribute() */ /** @ref */ TValue value
)
JScript
JScript 不支持通过引用传递值类型参数。

参数

key

要获取的值的键。

value

当此方法返回值时,如果找到该键,便会返回与指定的键相关联的值;否则,则会返回 value 参数的类型默认值。该参数未经初始化即被传递。

返回值

如果 Dictionary 包含具有指定键的元素,则为 true;否则为 false
异常类型 条件

ArgumentNullException

key 为 空引用(在 Visual Basic 中为 Nothing)。

此方法将 Item 属性的功能结合在一起。

如果未找到键,value 参数便为值类型 TValue 获取适当的默认值;例如,为 integer 类型获取 0(零),为布尔值类型获取 false,为引用类型获取 空引用(在 Visual Basic 中为 Nothing)。

如果代码频繁尝试访问字典中不存在的键,则可使用 TryGetValue 方法。使用此方法比捕获 Item 属性引发的 KeyNotFoundException 更为有效。

此方法的运算复杂度接近 O(1)。

该示例演示当程序频繁尝试字典中不存在的键时,如何使用 TryGetValue 方法来作为检索值的一种更有效的方法。为了对比,该示例还演示 Item 属性(在 C# 中为索引器)在试图检索不存在的键时如何引发异常。

此代码示例摘自一个为 Dictionary 类提供的更大的示例。

 1Dictionary.TryGetValue 方法 试用记 Mark     // When a program often has to try keys that turn out not to
 2Dictionary.TryGetValue 方法 试用记 Mark        // be in the dictionary, TryGetValue can be a more efficient 
 3Dictionary.TryGetValue 方法 试用记 Mark        // way to retrieve values.
 4Dictionary.TryGetValue 方法 试用记 Mark        string value = "";
 5Dictionary.TryGetValue 方法 试用记 Mark        if (openWith.TryGetValue("tif"out value))
 6

相关文章:

  • 2021-07-18
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2021-10-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
  • 2021-09-18
  • 2022-12-23
相关资源
相似解决方案