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
Signature | Access | Summary |
---|---|---|
protected DictionaryBase() | protected | Initializes a new instance of the DictionaryBase class. |
Properties
Name | Signature | Access | Summary |
---|---|---|---|
Dictionary | protected IDictionary Dictionary { get; } | protected | Gets the underlying dictionary that stores the key/value pairs. |
IsReadOnly | public bool IsReadOnly { get; } | public | Gets a value indicating whether the DictionaryBase is read-only. |
Keys | public ICollection Keys { get; } | public | Gets an DictionaryBase . |
Values | public ICollection Values { get; } | public | Gets an DictionaryBase . |
Methods
Name | Signature | Access | Summary |
---|---|---|---|
Add | public void Add(object key, object value) | public | Adds an element with the specified key and value into the DictionaryBase . |
Clear | public void Clear() | public | Removes all elements from the DictionaryBase . |
Contains | public bool Contains(object key) | public | Determines whether the DictionaryBase contains a specific key. |
GetEnumerator | public IDictionaryEnumerator GetEnumerator() | public | Returns an enumerator that iterates through the DictionaryBase . |
Remove | public void Remove(object key) | public | Removes the element with the specified key from the DictionaryBase . |
OnClear | protected virtual void OnClear() | protected | Raises the Clear event. |
OnInsert | protected virtual void OnInsert(object key, object value) | protected | Raises the Insert event. |
OnRemove | protected virtual void OnRemove(object key, object value) | protected | Raises the Remove event. |
OnSet | protected virtual void OnSet(object key, object oldValue, object newValue) | protected | Raises 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}");
}
}