:-)
おかげ。だから、あなたがコレクションに新しいキーと値のペアを追加した場合、あなたは、単にそうする前に、キーが存在するかどうかを確認することができます
static bool AddIfNotContainsKey<K,V>(this Dictionary<K,V> dict, K key, V value)
{
if (!dict.ContainsKey(key))
{
dict.Add(key, value);
return true;
}
return false;
}
例:
var dict = new Dictionary<string, string>();
dict.AddIfNotContainsKey("123456", "UV"); // returns true
dict.AddIfNotContainsKey("654321", "HV"); // returns true
dict.AddIfNotContainsKey("123456", "??"); // returns false
string result = dict["123456"]; // result == "UV"
出典
2011-07-12 15:07:04
dtb
ハッシュテーブルのキーは一意です。 – SLaks
'HashTable'クラスは非推奨です。その代わりに 'System.Collections.Generic'の汎用の' Dictionary'クラスを使用してください。 –