DictionaryBase Class Abstract

Provides a base implementation of the IDictionary interface to facilitate the creation of custom dictionary collections. This class is abstract and must be inherited.

Syntax

public abstract class DictionaryBase : ICollection, IEnumerable, IDictionary

Inheritance

Members

Properties

public virtual object this[object key] { get; }

Gets or sets the element with the specified key.

public virtual System.Collections.ICollection Keys { get; }

Gets an ICollection object that contains the keys to all the elements in the DictionaryBase object.

public virtual System.Collections.ICollection Values { get; }

Gets an ICollection object that contains the values to all the elements in the DictionaryBase object.

Methods

protected virtual void Add(object key, object value)

Adds an element with the specified key and value to the DictionaryBase instance.

protected virtual void Clear()

Removes all elements from the DictionaryBase instance.

protected virtual bool Contains(object key)

Determines whether the DictionaryBase instance contains an element with the specified key.

protected virtual void Remove(object key)

Removes the element with the specified key from the DictionaryBase instance.

protected virtual IDictionaryEnumerator GetEnumerator()

Returns an IDictionaryEnumerator object that can iterate through the DictionaryBase instance.

Remarks

The DictionaryBase class is designed for creating custom dictionary types that extend the functionality of a standard dictionary. It provides abstract methods and protected virtual methods that can be overridden to customize behavior. The underlying data store for the dictionary is an ArrayList, which is managed internally by the DictionaryBase class.

To create a custom dictionary class, you must inherit from DictionaryBase and implement the IDictionary interface. The DictionaryBase class provides base implementations for many of the IDictionary members, allowing you to focus on the specific logic of your custom dictionary.

Example

The following example demonstrates how to create a custom dictionary class by inheriting from DictionaryBase.

using System;
using System.Collections;

public class CustomDictionary : DictionaryBase
{
    public CustomDictionary()
    {
        // Constructor logic if needed
    }

    // Implement IDictionary members by overriding protected virtual methods
    // or by providing custom implementations.

    public override void Add(object key, object value)
    {
        // Custom validation or logic before adding
        if (key == null)
        {
            throw new ArgumentNullException(nameof(key));
        }
        base.Add(key, value); // Call the base implementation
    }

    public new object this[object key]
    {
        get { return base[key]; }
        set { base[key] = value; } // Note: DictionaryBase's indexer is get-only by default
    }

    public void AddCustom(string key, string value)
    {
        Add(key, value);
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        CustomDictionary myDict = new CustomDictionary();
        myDict.Add("FirstName", "Jane");
        myDict.Add("LastName", "Doe");

        Console.WriteLine($"Name: {myDict["FirstName"]} {myDict["LastName"]}");

        foreach (DictionaryEntry entry in myDict)
        {
            Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
        }
    }
}