Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 测试字典KeyNotFoundException_C# - Fatal编程技术网

C# 测试字典KeyNotFoundException

C# 测试字典KeyNotFoundException,c#,C#,我有一个场景,其中有一个字典在给定的时间可能有一个键值,也可能没有。我目前正在测试该值是否以以下方式存在,但我想知道这是否是最好的方法,或者是否有更好的方法来处理此问题 int myInt; try { myInt = {Value From Dictionary}; } catch { myInt = 0; } 有什么意见吗?谢谢。看看字典的方法 一些人建议使用ContainsKey。如果您确实想要该值,这不是一个好主意,因为它将意味着2次查找-例如 if (_myDict

我有一个场景,其中有一个字典在给定的时间可能有一个键值,也可能没有。我目前正在测试该值是否以以下方式存在,但我想知道这是否是最好的方法,或者是否有更好的方法来处理此问题

int myInt;

try
{
    myInt = {Value From Dictionary};
}
catch
{
    myInt = 0;
}

有什么意见吗?谢谢。

看看字典的方法

一些人建议使用ContainsKey。如果您确实想要该值,这不是一个好主意,因为它将意味着2次查找-例如

if (_myDictionary.ContainsKey(key)) // look up 1
{
 myInt = _myDictionary[key]; // look up 2
}

如果您谈论的是泛型词典,那么避免异常的最佳方法是使用ContainsKey方法在使用词典之前测试词典是否有键。

下面是一个示例

 using System;
 using System.Collections.Generic;

class Program
{
 static void Main()
 {
  Dictionary<string, string> test = new Dictionary<string, string>();
  test.Add("one", "value");
//
// Use TryGetValue to avoid KeyNotFoundException.
//
string value;
if (test.TryGetValue("two", out value))
{
    Console.WriteLine("Found");
}
else
{
    Console.WriteLine("Not found");
}
  }
}
使用系统;
使用System.Collections.Generic;
班级计划
{
静态void Main()
{
字典测试=新字典();
测试。添加(“一”、“值”);
//
//使用TryGetValue可避免KeyNotFoundException。
//
字符串值;
if(测试TryGetValue(“两个”,输出值))
{
控制台。写入线(“找到”);
}
其他的
{
控制台。写入线(“未找到”);
}
}
}

首先,在这里使用
try catch
不是一个好主意,您不必要地减慢了代码的速度,您可以使用
ContainsKey
TryGetValue

我将用这里提到的
TryGetValue
提出解决方案-(检查示例)

但您可以进行更多优化。行
myInt=0是冗余的,如@Mark建议的
TyGetValue
在返回时自动将
默认值(
0
用于
int

如果找不到键,则value参数获取类型TValue的适当默认值;例如,0(零)表示整数类型,false表示布尔类型,null表示引用类型。

所以最终的代码可能是

int myInt;
if (_myDictionary.TryGetValue(key, out myInt))
{
    [...] //codes that uses the value
}else{
    [...] //codes that does not use the value
}

下一段是从文件otTryGetValue-

此方法结合了ContainsKey方法和 项属性。如果找不到键,则value参数 获取类型TValue的适当默认值;例如,0 (零)表示整数类型,false表示布尔类型,null表示 引用类型。如果您经常使用代码,请使用TryGetValue方法 尝试访问字典中没有的密钥使用此 方法比捕获抛出的KeyNotFoundException更有效 通过项属性。此方法接近O(1)操作


BTW
ContainsKey
TryGetValue
都有运行时间O(1)。因此,这并不重要,您可以使用任何。

if(dict.ContainsValue(value))
或check key
dict.ContainsKey(value)
如果您不考虑是否提取值,
TryGetValue
更有意义。ContainsKey和TryGetValue都有运行时间~O(1)。但是如果你使用前者,然后使用索引器来得到你的值,那么你有2xo(n)。因此,正如我在回答中所说的,如果要使用该值,TryGetValue是更好的方法。TryGetValue不会执行任何魔术来获得这些值。它使用了前面提到的两个选项——“此方法结合了ContainsKey方法和Item属性的功能”。您隐式地使用了这两种方法。顺便说一句,它不是2xo(n),而是O(2n)~O(n),其中n=1“功能性”并不意味着等价。实际上,
TryGetValue
在索引器周围使用try/catch。因此,只有一个查找。检查反射器。
int myInt;
if (_myDictionary.TryGetValue(key, out myInt))
{
    [...] //codes that uses the value
}else{
    [...] //codes that does not use the value
}
int myInt;
_myDictionary.TryGetValue(key, out myInt))
[...] //other codes.