1
if (name == null || name == "")
2
return null;
3
XLine obj;
4
if (xlines.TryGetValue(name, out obj))
5
{
6
return obj;
7
}
8
else
9
{
10
return null;
11
}
在盖次的MSDN里找到一些东东.![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
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 中)
![](http://i.msdn.microsoft.com/Platform/Controls/CollapsibleArea/resources/plus.gif)
![](http://i.msdn.microsoft.com/Platform/Controls/CollapsibleArea/resources/minus.gif)
此方法将 ContainsKey 方法与 Item 属性的功能结合在一起。
如果未找到键,value 参数便为值类型 TValue 获取适当的默认值;例如,为 integer 类型获取 0(零),为布尔值类型获取 false,为引用类型获取 空引用(在 Visual Basic 中为 Nothing)。
如果代码频繁尝试访问字典中不存在的键,则可使用 TryGetValue 方法。使用此方法比捕获 Item 属性引发的 KeyNotFoundException 更为有效。
此方法的运算复杂度接近 O(1)。
![](http://i.msdn.microsoft.com/Platform/Controls/CollapsibleArea/resources/minus.gif)
该示例演示当程序频繁尝试字典中不存在的键时,如何使用 TryGetValue 方法来作为检索值的一种更有效的方法。为了对比,该示例还演示 Item 属性(在 C# 中为索引器)在试图检索不存在的键时如何引发异常。
此代码示例摘自一个为 Dictionary 类提供的更大的示例。
1
// When a program often has to try keys that turn out not to
2
// be in the dictionary, TryGetValue can be a more efficient
3
// way to retrieve values.
4
string value = "";
5
if (openWith.TryGetValue("tif", out value))
6
{
7
Console.WriteLine("For key = \"tif\", value = {0}.", value);
8
}
9
else
10
{
11
Console.WriteLine("Key = \"tif\" is not found.");
12
}
13
<br /><span space="preserve">
</span><br /> // The indexer throws an exception if the requested key is
14
// not in the dictionary.
15
try
16
{
17
Console.WriteLine("For key = \"tif\", value = {0}.",
18
openWith["tif"]);
19
}
20
catch (KeyNotFoundException)
21
{
22
Console.WriteLine("Key = \"tif\" is not found.");
23
}
24![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)