Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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# 如何避免字典中的空键错误?_C#_Static Methods - Fatal编程技术网

C# 如何避免字典中的空键错误?

C# 如何避免字典中的空键错误?,c#,static-methods,C#,Static Methods,如果密钥为空,如何避免错误 //Getter/setter public static Dictionary<string, string> Dictionary { get { return Global.dictionary; } set { Global.dictionary = value; } } //Getter/setter 公共静态词典 { 获取{return Global.dictionary;} 设置{Global.dictionary=valu

如果密钥为空,如何避免错误

//Getter/setter
public static Dictionary<string, string> Dictionary
{
    get { return Global.dictionary; }
    set { Global.dictionary = value; }
}
//Getter/setter
公共静态词典
{
获取{return Global.dictionary;}
设置{Global.dictionary=value;}
}
更新:

Dictionary.Add("Key1", "Text1");
Dictionary["Key2"] <-error! so what can I write in the GET to avoid error?
Dictionary.Add(“Key1”、“Text1”);

字典[“Key2”]字典中的键永远不能为空。字典是一个哈希表,根据定义,您需要一个非空键,或者哈希函数无法映射到相应的元素。

您可以使用该方法

所以你会写:

if (myDictionary.ContainsKey("Key2"))
{
    // Do something.
}
其他替代方法是将访问包装在
try…catch
块中或使用(参见链接到的MSDN页面上的示例)

如果您希望对结果执行某些操作,则
TryGetMethod
更有效,因为您不需要第二次调用来获取值(与
ContainsKey
方法一样)


(当然,在这两种方法中,您都可以用变量替换“Key2”)

使用
TryGetValue

Dictionary<int, string> dict = ...;
string value;

if (dict.TryGetValue(key, out value))
{
    // value found
    return value;
}
else
{
    // value not found, return what you want
}
Dictionary dict=。。。;
字符串值;
if(dict.TryGetValue(键,输出值))
{
//价值发现
返回值;
}
其他的
{
//未找到值,返回所需内容
}

您返回了错误的内容。不要返回字典,传入一个键并返回值

public static string GetValue(string key)
{
    if(Global.dictionary.ContainsKey(key))
    {
        return Global.dictionary[key];
    }

    return ""; // or some other value
}
扩展方法:

public static TValue GetValue<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key)
{
    TValue result;
    return dic.TryGetValue(key, out result) ?
        result :
        default(TValue);
}
公共静态TValue GetValue(此字典dic,TKey)
{
t价值结果;
返回dic.TryGetValue(键,输出结果)?
结果:
默认值(TValue);
}
用法:

var dic = new Dictionary<string, string>
{
   { "key", "value" }
};

string r1 = dic.GetValue("key"); // "value"
string r2 = dic.GetValue("false"); // null
var dic=新字典
{
{“键”,“值”}
};
字符串r1=dic.GetValue(“键”);//“价值”
字符串r2=dic.GetValue(“false”);//无效的

如何检查它是否为null并返回一个空字符串?我认为这是他的问题,如何避免将
null
设置为键…但是,他的代码不符合该假设。在代码中,您从不按键访问字典。您只需返回一个静态字典。我不明白你的意思。@Bobby,这永远不会发生。如果你试图在字典中使用null作为键,你将得到ArgumentNullException,这样你就永远不会有一个带有null键的字典。是的,我想这是他的问题,如何避免这种异常。代码不适合这个问题。。。你的钥匙在哪里?我不是说在方法中,而是在声明中@伊曼-我不明白你的问题。您能否更新代码以准确显示您试图实现的目标?+1使用try方法,如果要检索值,请不要使用contains方法,如果答案不正确,
TryGetValue
会在键为空时引发异常。@JasonBaley不确定这与答案有关,
字典
不支持空键。更多的讨论可以在这个线程中找到:@Grozz问题是当在字典中提供空键进行查找时,如何避免出现错误。将null键传递给
TryGetValue
仍将抛出错误,因此它不会回答问题。在您的回答中,如果
key
为null,则会引发异常,那么这是如何解决原始问题的?他试图避免出现例外情况。@JasonBaley请仔细阅读整个问题,而不仅仅是标题
var dic = new Dictionary<string, string>
{
   { "key", "value" }
};

string r1 = dic.GetValue("key"); // "value"
string r2 = dic.GetValue("false"); // null