基本上,我想要这样的东西:
Dictionary<object, string> dict = new Dictionary<object, string>();
dict.Add(null, "Nothing");
dict.Add(1, "One");
是否有任何内置的基类库允许这样做?上述代码在添加空键时会在运行时抛出异常。
基本上,我想要这样的东西:
Dictionary<object, string> dict = new Dictionary<object, string>();
dict.Add(null, "Nothing");
dict.Add(1, "One");
是否有任何内置的基类库允许这样做?上述代码在添加空键时会在运行时抛出异常。
您可以避免使用 null 并创建一个特殊的单例值类来做同样的事情。例如:
public sealed class Nothing
{
public static readonly Nothing Value = new Nothing();
private Nothing() {}
}
Dictionary<object, string> dict = new Dictionary<object, string>();
dict.add(Nothing.Value, "Nothing");
dict.add(1, "One");
如果您打算使您的集合具有更强的类型,这种方法将无法工作 - 例如,您希望键是一个字符串。由于字符串是密封的,因此您不能从它继承来创建一个“特殊值”替代 null。你的选择变得有点复杂。你可以:
顺便说一句,您的字典键真的需要键object
吗?由于在您可能打算将 Equals() 评估为比较基础的地方使用了引用相等,这可能会导致细微的错误。
这个怎么样?
public class NullableDictionnary<T1, T2> : Dictionary<T1, T2>
{
T2 null_value;
public T2 this[T1 key]
{
get
{
if (key == null)
{ return null_value; }
return base[key];
}
set
{
if (key == null)
{ null_value = value; }
else
{ base[key] = value; }
}
}
}
NameValueCollection 可以采用空键,但它不实现 IDictionary。然而,从 DictionaryBase 派生并提供 Add/Remove/Indexers 等将非常容易,只需将 null 替换为内置的内容,例如:
class MyDictionary : DictionaryBase {
private readonly object nullKey = new object();
void Add(object key, string value) {
if ( key == null ) { key = nullKey; }
.. call base methods
}
}
不需要字典的不同实现。
看看我的答案: https ://stackoverflow.com/a/22261282/212272
您还可以保持字典的强类型:
var dict = new Dictionary<NullObject<int?>, string>();
dict[1] = "one int";
dict[null] = "null int";
Assert.AreEqual("one int", dict[1]);
Assert.AreEqual("null int", dict[null]);
键字面上是否需要为 NULL?集合中的键是一个索引。集合中的索引为 NULL 对我来说没有多大意义。
也许创建一个新类
public class ObjectEntry
{
public object objRef;
public string desc;
public ObjectEntry(object objectReference)
{
objRef = objectReference;
if (objRef = null) {desc = "Nothing";}
else {desc = objRef.Description;} //or whatever info you can get from a proper objRef value
}
}
newObj = new ObjectEntry(null);
dict.add(newObj, newObj.desc);
You can simply use ValueTuple as a wrapper for key, for example:
Dictionary<ValueTuple<string?>, string>
对 jestro 的回答略有不同,以提供更清洁的(对我而言)解决方案,使您想要做的事情更加明确。显然,这可以根据需要进行扩展。但是你得到了图片,只需做一个包装。
public class NullDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
private TValue _default;
public new TValue this[TKey key]
{
get {
if(key == null)
{
return _default;
}
return _decorated[key];
}
}
private Dictionary<TKey, TValue> _decorated;
public NullDictionary( Dictionary<TKey,TValue> decorate, TValue defaultValue = default)
{
_decorated = decorate;
_default = defaultValue;
}
}