Dictionary.TryGetValue 方法 试用记 Mark

摘要:
/**@AttributeOutAttribute()*//**@ref*/TValue)JScriptJScript不支持通过引用传递值类型参数。参数键要获取的值的键。返回value参数的类型默认值。value参数获取值类型TValue的适当默认值;布尔类型为false。此示例还演示了Item属性(C#中的索引器)在尝试检索不存在的键时如何引发异常。
 1Dictionary.TryGetValue 方法 试用记 Mark第1张 if (name == null || name == "")
 2Dictionary.TryGetValue 方法 试用记 Mark第1张                return null;
 3Dictionary.TryGetValue 方法 试用记 Mark第1张            XLine obj;
 4Dictionary.TryGetValue 方法 试用记 Mark第1张            if (xlines.TryGetValue(name, out obj))
 5Dictionary.TryGetValue 方法 试用记 Mark第5张Dictionary.TryGetValue 方法 试用记 Mark第6张            Dictionary.TryGetValue 方法 试用记 Mark第7张{
 6Dictionary.TryGetValue 方法 试用记 Mark第8张                return obj;
 7Dictionary.TryGetValue 方法 试用记 Mark第9张            }

 8Dictionary.TryGetValue 方法 试用记 Mark第1张            else
 9Dictionary.TryGetValue 方法 试用记 Mark第5张Dictionary.TryGetValue 方法 试用记 Mark第6张            Dictionary.TryGetValue 方法 试用记 Mark第7张{
10Dictionary.TryGetValue 方法 试用记 Mark第8张                return null;
11Dictionary.TryGetValue 方法 试用记 Mark第9张            }
在盖次的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 中)

Dictionary.TryGetValue 方法 试用记 Mark第16张 语法
Visual Basic(声明)
Public Function TryGetValue ( _
key As TKey, _
<OutAttribute> ByRef value As TValue _
) As Boolean
Visual Basic(用法)
Dim instance As Dictionary(Of TKey, TValue)
Dim key As TKey
Dim value As TValue
Dim returnValue As Boolean
returnValue = instance.TryGetValue(key, value)
C#
public 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
Dictionary.TryGetValue 方法 试用记 Mark第17张 异常
异常类型条件

ArgumentNullException

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

Dictionary.TryGetValue 方法 试用记 Mark第17张 备注

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

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

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

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

Dictionary.TryGetValue 方法 试用记 Mark第17张 示例

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

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

 1Dictionary.TryGetValue 方法 试用记 Mark第1张     // When a program often has to try keys that turn out not to
 2Dictionary.TryGetValue 方法 试用记 Mark第1张        // be in the dictionary, TryGetValue can be a more efficient 
 3Dictionary.TryGetValue 方法 试用记 Mark第1张        // way to retrieve values.
 4Dictionary.TryGetValue 方法 试用记 Mark第1张        string value = "";
 5Dictionary.TryGetValue 方法 试用记 Mark第1张        if (openWith.TryGetValue("tif"out value))
 6Dictionary.TryGetValue 方法 试用记 Mark第5张Dictionary.TryGetValue 方法 试用记 Mark第6张        Dictionary.TryGetValue 方法 试用记 Mark第7张{
 7Dictionary.TryGetValue 方法 试用记 Mark第8张            Console.WriteLine("For key = \"tif\", value = {0}.", value);
 8Dictionary.TryGetValue 方法 试用记 Mark第9张        }

 9Dictionary.TryGetValue 方法 试用记 Mark第1张        else
10Dictionary.TryGetValue 方法 试用记 Mark第5张Dictionary.TryGetValue 方法 试用记 Mark第6张        Dictionary.TryGetValue 方法 试用记 Mark第7张{
11Dictionary.TryGetValue 方法 试用记 Mark第8张            Console.WriteLine("Key = \"tif\" is not found.");
12Dictionary.TryGetValue 方法 试用记 Mark第9张        }

13Dictionary.TryGetValue 方法 试用记 Mark第1张<br /><span space="preserve">Dictionary.TryGetValue 方法 试用记 Mark第7张</span><br />        // The indexer throws an exception if the requested key is
14Dictionary.TryGetValue 方法 试用记 Mark第1张        // not in the dictionary.
15Dictionary.TryGetValue 方法 试用记 Mark第1张        try
16Dictionary.TryGetValue 方法 试用记 Mark第5张Dictionary.TryGetValue 方法 试用记 Mark第6张        Dictionary.TryGetValue 方法 试用记 Mark第7张{
17Dictionary.TryGetValue 方法 试用记 Mark第8张            Console.WriteLine("For key = \"tif\", value = {0}."
18Dictionary.TryGetValue 方法 试用记 Mark第8张                openWith["tif"]);
19Dictionary.TryGetValue 方法 试用记 Mark第9张        }

20Dictionary.TryGetValue 方法 试用记 Mark第1张        catch (KeyNotFoundException)
21Dictionary.TryGetValue 方法 试用记 Mark第5张Dictionary.TryGetValue 方法 试用记 Mark第6张        Dictionary.TryGetValue 方法 试用记 Mark第7张{
22Dictionary.TryGetValue 方法 试用记 Mark第8张            Console.WriteLine("Key = \"tif\" is not found.");
23Dictionary.TryGetValue 方法 试用记 Mark第9张        }

24Dictionary.TryGetValue 方法 试用记 Mark第1张

免责声明:文章转载自《Dictionary.TryGetValue 方法 试用记 Mark》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JAVA中获取文件MD5值的方法Java判断IP地址类型(第二版)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

performSelector

perfromSelector 底层源码地址:https://opensource.apple.com/tarballs/objc4/ 非延迟方法 - (id)performSelector:(SEL)sel { if (!sel) [self doesNotRecognizeSelector:sel]; return ((id(*)(id...

RequestBodyAdvice和ResponseBodyAdvice详解,@ControllerAdvice注解

一、源码解析 这是spring 4.2新加的两个接口 1、RequestBodyAdvice public interface RequestBodyAdvice { boolean supports(MethodParameter var1, Type var2, Class<? extends HttpMessageConverter&l...

关于 SetProcessWorkingSetSize 和内存释放

在应用程序中,往往为了释放内存等,使用一些函数,其实,对于内存操作函数要谨慎使用,比如大家常常想到的 SetProcessWorkingSetSize,其实对于windows来说,系统会自动在程序闲置时(如程序被最小化)释放内存的,自己用内存释放 时,往往会造成一些莫名的内存错误,造成自己的应用程序及系统不稳定。 具体原理有人已经写得很清楚了,以下为转帖的...

python的接口和抽象类

抽象基类有些面向对象的语言,如JAVA,支持接口,可以声明一个支持给定的一些方法方法,或者支持给定存取协议的类。抽象基类(或者ABCs)是Python里一个相同的特性。抽象基类由abc模块构成,包含了一个叫做ABCMeta的metaclass。这个metaclass由内置的isinstance()和issubclass()特别处理,并包含一批会被Pytho...

3月18日 winform页面设置与打印

1.页面设置(PageSetupDialog) pageSetupDialog1.Document = printDocument1;    //必须设置打印对象,要不然不知道打印谁    pageSetupDialog1.ShowDialog();                    //ShowDialog是打印这一栏里都有的方法 执行结果: 2...

java时间API,SpringBoot中应用LocalDateTime(日期转换)

参考:JDK8的LocalDateTime用法 参考资料:好好学Java  https://mp.weixin.qq.com/s/Dd_7yUh3lq3TqE2cjsYXvw JDK8新特性里提供了3个时间类:LocalDate、LocalTime、LocalDateTime 在项目开发中,已经需要对Date类型进行格式,否则可读性很差,格式化Date类型...