如何更改Dictionary以使其返回自定义默认值而不是在没有这样的键时抛出异常?

时间:2012-10-30 16:16:12

标签: c# .net dictionary

如何更改字典的工作方式,以便在没有给定密钥的KVP时,它会返回一个默认值,而不会使用try-catch包装常用的dic["nonexistentKey"]

4 个答案:

答案 0 :(得分:6)

您可以创建自己的封装 Dictionary<TKey,TValue>的类,并实现IDictionary<TKey,TValue>

这会像字典一样,但您可以编写行为来处理您不存在的密钥。

但是,您无法更改实际Dictionary<TKey,TValue>类的功能。

答案 1 :(得分:5)

如果您愿意,也可以向IDictionary或Dictionary添加扩展方法。

public static class IDictionaryExtensions
{
    public static TValue ValueAtOrDefault<TKey, TValue>(
        this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
    {
        if (dictionary == null || !dictionary.ContainsKey(key))
        {
            return defaultValue;
        }

        return dictionary[key];
    }
}

请注意,如果字典为null,您可能希望抛出ArgumentNullException,而不是像示例中那样返回默认值...适合您的任何内容。

答案 2 :(得分:3)

使用Dictionary.TryGetValue

Dictionary<int, YourType> dictionary = ...;
YourType x;
if (!dictionary.TryGetValue(123, out x))
{
    x = new YourType();
}

// here X will be assigned to the value or the default if the key was not present.

如果确实需要覆盖默认的dictionary[key]方法,则可以使用此类(作为字典本身或作为现有字典的包装器):

/// <summary>
/// A dictionary implementation that returns the default value of <typeparamref name="TValue"/> when the key is not present in the dictionary.
/// </summary>
public class DictionaryWithDefaults<TKey, TValue> : IDictionary<TKey, TValue>
{
    /// <summary>
    /// Holds the actual data using standard dictionary.
    /// </summary>
    private IDictionary<TKey, TValue> _storage;

    /// <summary>
    /// Initializes a new instance of the <see cref="DictionaryWithDefaults{TValue}" /> class.
    /// The data is stored directly in this dictionary.
    /// </summary>
    public DictionaryWithDefaults()
    {
        this._storage = new Dictionary<TKey, TValue>();
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="DictionaryWithDefaults{TValue}" /> class.
    /// This dictionary acts as a wrapper for the data stored in the dictionary <paramref name="forWrapping" />.
    /// </summary>
    /// <param name="forWrapping">The dictionary object for wrapping.</param>
    /// <exception cref="System.ArgumentNullException">when <paramref name="forWrapping"/> is <c>null</c></exception>
    public DictionaryWithDefaults(IDictionary<TKey, TValue> forWrapping)
    {
        if (forWrapping == null)
            throw new ArgumentNullException("forWrapping");

        this._storage = forWrapping;
    }

    public void Add(TKey key, TValue value)
    {
        this._storage.Add(key, value);
    }

    public bool ContainsKey(TKey key)
    {
        return this._storage.ContainsKey(key);
    }

    public ICollection<TKey> Keys
    {
        get { return this._storage.Keys; }
    }

    public bool Remove(TKey key)
    {
        return this._storage.Remove(key);
    }

    public bool TryGetValue(TKey key, out TValue value)
    {
        // always return a value, even if the key does not exist.
        // this is also the only place one would modify if the default value has to be customized (passed in the constructor etc.)
        if (!this._storage.TryGetValue(key, out value))
            value = default(TValue);

        return true;
    }

    public ICollection<TValue> Values
    {
        get { return this._storage.Values; }
    }

    public TValue this[TKey key]
    {
        get
        {
            TValue value;
            this.TryGetValue(key, out value);
            return value;
        }
        set
        {
            this._storage[key] = value;
        }
    }

    public void Add(KeyValuePair<TKey, TValue> item)
    {
        this._storage.Add(item);
    }

    public void Clear()
    {
        this._storage.Clear();
    }

    public bool Contains(KeyValuePair<TKey, TValue> item)
    {
        return this._storage.Contains(item);
    }

    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
    {
        this._storage.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get { return this._storage.Count; }
    }

    public bool IsReadOnly
    {
        get { return this._storage.IsReadOnly; }
    }

    public bool Remove(KeyValuePair<TKey, TValue> item)
    {
        return this._storage.Remove(item);
    }

    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
    {
        return this._storage.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this._storage.GetEnumerator();
    }
}

答案 3 :(得分:1)

不,你必须把它包起来。 this[]不是虚方法,无法覆盖。

所以你最好的办法是创建一个简单的类,它暴露IDictionary(或只选择一些方法)并用try / catch包装this[] get