Microsoft Docs
System.Collections.DictionaryBase

DictionaryBase Class

The DictionaryBase class provides an abstract base class for a strongly typed collection of key/value pairs.

Namespace: System.Collections

Assembly: System.Collections.dll

Syntax

public abstract class DictionaryBase : ICollection, IDictionary, IEnumerable, ISerializable, IDeserializationCallback

Members

Constructors

SignatureAccessSummary
protected DictionaryBase()protectedInitializes a new instance of the DictionaryBase class.

Properties

NameSignatureAccessSummary
Dictionaryprotected IDictionary Dictionary { get; }protectedGets the underlying dictionary that stores the key/value pairs.
IsReadOnlypublic bool IsReadOnly { get; }publicGets a value indicating whether the DictionaryBase is read-only.
Keyspublic ICollection Keys { get; }publicGets an containing the keys of the DictionaryBase.
Valuespublic ICollection Values { get; }publicGets an containing the values of the DictionaryBase.

Methods

NameSignatureAccessSummary
Addpublic void Add(object key, object value)publicAdds an element with the specified key and value into the DictionaryBase.
Clearpublic void Clear()publicRemoves all elements from the DictionaryBase.
Containspublic bool Contains(object key)publicDetermines whether the DictionaryBase contains a specific key.
GetEnumeratorpublic IDictionaryEnumerator GetEnumerator()publicReturns an enumerator that iterates through the DictionaryBase.
Removepublic void Remove(object key)publicRemoves the element with the specified key from the DictionaryBase.
OnClearprotected virtual void OnClear()protectedRaises the Clear event.
OnInsertprotected virtual void OnInsert(object key, object value)protectedRaises the Insert event.
OnRemoveprotected virtual void OnRemove(object key, object value)protectedRaises the Remove event.
OnSetprotected virtual void OnSet(object key, object oldValue, object newValue)protectedRaises the Set event.

Example

The following example demonstrates how to derive a custom dictionary from DictionaryBase that enforces string keys and integer values.

using System;
using System.Collections;

public class StringIntDictionary : DictionaryBase
{
    public int this[string key]
    {
        get { return (int)Dictionary[key]; }
        set { Dictionary[key] = value; }
    }

    public void Add(string key, int value)
    {
        OnValidate(key, value);
        Dictionary.Add(key, value);
    }

    public bool Contains(string key)
    {
        return Dictionary.Contains(key);
    }

    public void Remove(string key)
    {
        Dictionary.Remove(key);
    }

    protected override void OnValidate(object key, object value)
    {
        if (key == null) throw new ArgumentNullException(nameof(key));
        if (!(key is string)) throw new ArgumentException("Key must be a string.");
        if (!(value is int)) throw new ArgumentException("Value must be an integer.");
    }

    public ICollection Keys => Dictionary.Keys;
    public ICollection Values => Dictionary.Values;
}

class Program
{
    static void Main()
    {
        var dict = new StringIntDictionary();
        dict.Add("Apples", 5);
        dict.Add("Oranges", 10);
        Console.WriteLine($"Apples: {dict["Apples"]}");
        Console.WriteLine($"Count: {dict.Count}");
    }
}

See Also