System.Collections.DictionaryBase Class

Assembly: Mscorlib.dll
Namespace: System.Collections
Summary
Provides the abstract base class for a strongly typed collection of key-and-value pairs.
C# Syntax:
[Serializable]
public abstract class DictionaryBase : IDictionary, ICollection, IEnumerable
Thread Safety
Public static (non-instance) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

This implementation does not provide a synchronized (thread-safe) wrapper for a DictionaryBase, but derived classes can create their own synchronized versions of the DictionaryBase using the DictionaryBase.System.Collections.ICollection.SyncRoot property.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Remarks
Each element is a key-and-value pair stored in a DictionaryEntry object.

The foreach statement of the C# language requires the type of each element in the collection. Since each element of the DictionaryBase is a key-and-value pair, the element type is not the type of the key or the type of the value. Instead, the element type is DictionaryEntry. For example: foreach (DictionaryEntry myEntry in myDictionaryBase) {...}



Notes to implementors:

This base class is provided to make it easier for implementers to create a strongly typed custom collection. Implementers should extend this base class instead of creating their own.

Members of this base class are protected and are intended to be used through a derived class only.

See also:
System.Collections Namespace | Hashtable | IDictionary

System.Collections.DictionaryBase Member List:

Public Properties
Count Read-only

Gets the number of elements contained in the DictionaryBase instance.
Public Methods
Clear Clears the contents of the DictionaryBase instance.
CopyTo Copies the DictionaryBase elements to a one-dimensional Array at the specified index.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetEnumerator Returns an IDictionaryEnumerator that can iterate through the DictionaryBase instance.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Protected Properties
Dictionary Read-only

Gets the list of elements contained in the DictionaryBase instance.
InnerHashtable Read-only

Gets the list of elements contained in the DictionaryBase instance.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.
OnClear Performs additional custom processes before clearing the contents of the DictionaryBase instance.
OnClearComplete Performs additional custom processes after clearing the contents of the DictionaryBase instance.
OnGet Gets the element with the specified key and value in the DictionaryBase instance.
OnInsert Performs additional custom processes before inserting a new element into the DictionaryBase instance.
OnInsertComplete Performs additional custom processes after inserting a new element into the DictionaryBase instance.
OnRemove Performs additional custom processes before removing an element from the DictionaryBase instance.
OnRemoveComplete Performs additional custom processes after removing an element from the DictionaryBase instance.
OnSet Performs additional custom processes before setting a value in the DictionaryBase instance.
OnSetComplete Performs additional custom processes after setting a value in the DictionaryBase instance.
OnValidate Performs additional custom processes when validating the element with the specified key and value.
Explicit Interface Implementations 
IDictionary.Add Adds an element with the specified key and value into the DictionaryBase.
IDictionary.Contains Determines whether the DictionaryBase contains a specific key.
IDictionary.Remove Removes the element with the specified key from the DictionaryBase.
IEnumerable.GetEnumerator Returns an IEnumerator that can iterate through the DictionaryBase.

Hierarchy:


System.Collections.DictionaryBase Member Details

ctor #1
Summary:
Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
protected DictionaryBase();

Return to top


Property: Count (read-only)
Summary
Gets the number of elements contained in the DictionaryBase instance.
C# Syntax:
public int Count {get;}
Implements:
ICollection.Count

Return to top


Property: Dictionary (read-only)
Summary
Gets the list of elements contained in the DictionaryBase instance.
C# Syntax:
protected IDictionary Dictionary {get;}
Remarks
The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.

Return to top


Property: InnerHashtable (read-only)
Summary
Gets the list of elements contained in the DictionaryBase instance.
C# Syntax:
protected Hashtable InnerHashtable {get;}
Remarks
The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.

Return to top


Method: Clear()
Summary
Clears the contents of the DictionaryBase instance.
C# Syntax:
public void Clear();
Implements:
IDictionary.Clear
Remarks
DictionaryBase.Count is set to zero.

Return to top


Method: CopyTo(
   Array array,
   int index
)
Summary
Copies the DictionaryBase elements to a one-dimensional Array at the specified index.
C# Syntax:
public void CopyTo(
   Array array,
   int index
);
Parameters:

array

The one-dimensional Array that is the destination of the DictionaryEntry objects copied from the DictionaryBase instance. The Array must have zero-based indexing.

index

The zero-based index in array at which copying begins.

Exceptions
Exception Type Condition
ArgumentNullException array is null.
ArgumentOutOfRangeException index is less than zero.
ArgumentException array is multidimensional.

-or-

index is equal to or greater than the length of array.

-or-

The number of elements in the source DictionaryBase is greater than the available space from index to the end of the destination array.

InvalidCastException The type of the source DictionaryBase cannot be cast automatically to the type of the destination array.
Implements:
ICollection.CopyTo
Remarks
The elements are copied to the Array in the same order in which the enumerator iterates through the DictionaryBase.
See also:
Array | DictionaryEntry | DictionaryBase.GetEnumerator

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~DictionaryBase();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetEnumerator()
Summary
Returns an IDictionaryEnumerator that can iterate through the DictionaryBase instance.
C# Syntax:
public IDictionaryEnumerator GetEnumerator();
Return Value:
An IDictionaryEnumerator for the DictionaryBase instance.
Implements:
IDictionary.GetEnumerator
Remarks
Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.

IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.

After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.

The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

See also:
IDictionaryEnumerator | IEnumerator

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: OnClear()
Summary
Performs additional custom processes before clearing the contents of the DictionaryBase instance.
C# Syntax:
protected virtual void OnClear();
Remarks
The default implementation of this method is intended to be overridden by a derived class to perform some action before the collection is cleared.

The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.



Notes to implementors:

This method allows implementers to define processes that must be performed before deleting all the elements from the underlying Hashtable. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.

DictionaryBase.OnClear is invoked before the standard Clear behavior, whereas DictionaryBase.OnClearComplete is invoked after the standard Clear behavior.

For example, implementers can exempt certain elements from deletion by a global Clear.

See also:
DictionaryBase.OnClearComplete | DictionaryBase.OnRemove

Return to top


Method: OnClearComplete()
Summary
Performs additional custom processes after clearing the contents of the DictionaryBase instance.
C# Syntax:
protected virtual void OnClearComplete();
Remarks
The default implementation of this method is intended to be overridden by a derived class to perform some action after the collection is cleared.

The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.



Notes to implementors:

This method allows implementers to define processes that must be performed after deleting all the elements from the underlying Hashtable. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.

DictionaryBase.OnClear is invoked before the standard Clear behavior, whereas DictionaryBase.OnClearComplete is invoked after the standard Clear behavior.

See also:
DictionaryBase.OnClear | DictionaryBase.OnRemoveComplete

Return to top


Method: OnGet(
   object key,
   object currentValue
)
Summary
Gets the element with the specified key and value in the DictionaryBase instance.
C# Syntax:
protected virtual object OnGet(
   object key,
   object currentValue
);
Parameters:

key

The key of the element to get.

currentValue

The current value of the element associated with key.

Return Value:
An Object containing the element with the specified key and value.
Remarks
The default implementation of this method returns currentValue. It is intended to be overridden by a derived class to perform additional action when the specified element is retrieved.

The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.



Notes to implementors:

This method allows implementers to define processes that must be performed when executing the standard Get behavior of the underlying Hashtable. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.

DictionaryBase.OnGet can be used to specify processes to perform before returning the value retrieved from the underlying Hashtable. For example, implementers can cast the value into another type before returning it.

Return to top


Method: OnInsert(
   object key,
   object value
)
Summary
Performs additional custom processes before inserting a new element into the DictionaryBase instance.
C# Syntax:
protected virtual void OnInsert(
   object key,
   object value
);
Parameters:

key

The key of the element to insert.

value

The value of the element to insert.

Remarks
The default implementation of this method is intended to be overridden by a derived class to perform some action before the specified element is inserted.

The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.



Notes to implementors:

This method allows implementers to define processes that must be performed before inserting the element into the underlying Hashtable. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.

DictionaryBase.OnInsert is invoked before the standard Insert behavior, whereas DictionaryBase.OnInsertComplete is invoked after the standard Insert behavior.

For example, implementers can restrict which types of objects can be inserted into the Hashtable.

See also:
DictionaryBase.OnInsertComplete | DictionaryBase.OnSet | DictionaryBase.OnValidate

Return to top


Method: OnInsertComplete(
   object key,
   object value
)
Summary
Performs additional custom processes after inserting a new element into the DictionaryBase instance.
C# Syntax:
protected virtual void OnInsertComplete(
   object key,
   object value
);
Parameters:

key

The key of the element to insert.

value

The value of the element to insert.

Remarks
The default implementation of this method is intended to be overridden by a derived class to perform some action after the specified element is inserted.

The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.



Notes to implementors:

This method allows implementers to define processes that must be performed after inserting the element into the underlying Hashtable. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.

DictionaryBase.OnInsert is invoked before the standard Insert behavior, whereas DictionaryBase.OnInsertComplete is invoked after the standard Insert behavior.

See also:
DictionaryBase.OnInsert | DictionaryBase.OnSetComplete

Return to top


Method: OnRemove(
   object key,
   object value
)
Summary
Performs additional custom processes before removing an element from the DictionaryBase instance.
C# Syntax:
protected virtual void OnRemove(
   object key,
   object value
);
Parameters:

key

The key of the element to remove.

value

The value of the element to remove.

Remarks
The default implementation of this method is intended to be overridden by a derived class to perform some action before the specified element is removed.

The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.



Notes to implementors:

This method allows implementers to define processes that must be performed before removing the element from the underlying Hashtable. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.

DictionaryBase.OnRemove is invoked before the standard Remove behavior, whereas DictionaryBase.OnRemoveComplete is invoked after the standard Remove behavior.

For example, implementers can prevent removal of elements by always throwing an exception in DictionaryBase.OnRemove.

See also:
DictionaryBase.OnRemoveComplete | DictionaryBase.OnClear

Return to top


Method: OnRemoveComplete(
   object key,
   object value
)
Summary
Performs additional custom processes after removing an element from the DictionaryBase instance.
C# Syntax:
protected virtual void OnRemoveComplete(
   object key,
   object value
);
Parameters:

key

The key of the element to remove.

value

The value of the element to remove.

Remarks
The default implementation of this method is intended to be overridden by a derived class to perform some action after the specified element is removed.

The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.



Notes to implementors:

This method allows implementers to define processes that must be performed after removing the element from the underlying Hashtable. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.

DictionaryBase.OnRemove is invoked before the standard Remove behavior, whereas DictionaryBase.OnRemoveComplete is invoked after the standard Remove behavior.

See also:
DictionaryBase.OnRemove | DictionaryBase.OnClearComplete

Return to top


Method: OnSet(
   object key,
   object oldValue,
   object newValue
)
Summary
Performs additional custom processes before setting a value in the DictionaryBase instance.
C# Syntax:
protected virtual void OnSet(
   object key,
   object oldValue,
   object newValue
);
Parameters:

key

The key of the element to locate.

oldValue

The old value of the element associated with key.

newValue

The new value of the element associated with key.

Remarks
The default implementation of this method is intended to be overridden by a derived class to perform some action before the specified element is set.

The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.



Notes to implementors:

This method allows implementers to define processes that must be performed before setting the specified element in the underlying Hashtable. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.

DictionaryBase.OnSet is invoked before the standard Set behavior, whereas DictionaryBase.OnSetComplete is invoked after the standard Set behavior.

For example, implementers can restrict which values can be overwritten by performing a check inside DictionaryBase.OnSet.

See also:
DictionaryBase.OnSetComplete | DictionaryBase.OnInsert | DictionaryBase.OnValidate

Return to top


Method: OnSetComplete(
   object key,
   object oldValue,
   object newValue
)
Summary
Performs additional custom processes after setting a value in the DictionaryBase instance.
C# Syntax:
protected virtual void OnSetComplete(
   object key,
   object oldValue,
   object newValue
);
Parameters:

key

The key of the element to locate.

oldValue

The old value of the element associated with key.

newValue

The new value of the element associated with key.

Remarks
The default implementation of this method is intended to be overridden by a derived class to perform some action after the specified element is set.

The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.



Notes to implementors:

This method allows implementers to define processes that must be performed after setting the specified element in the underlying Hashtable. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.

DictionaryBase.OnSet is invoked before the standard Set behavior, whereas DictionaryBase.OnSetComplete is invoked after the standard Set behavior.

See also:
DictionaryBase.OnSet | DictionaryBase.OnInsertComplete

Return to top


Method: OnValidate(
   object key,
   object value
)
Summary
Performs additional custom processes when validating the element with the specified key and value.
C# Syntax:
protected virtual void OnValidate(
   object key,
   object value
);
Parameters:

key

The key of the element to validate.

value

The value of the element to validate.

Remarks
The default implementation of this method is intended to be overridden by a derived class to perform some action when the specified element is validated.

The On* methods are invoked only on the instance returned by the DictionaryBase.Dictionary property, but not on the instance returned by the DictionaryBase.InnerHashtable property.



Notes to implementors:

This method allows implementers to define processes that must be performed when executing the standard behavior of the underlying Hashtable. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.

DictionaryBase.OnValidate can be used to impose restrictions on the type of objects that are accepted into the collection. The default implementation prevents null from being added to or removed from the underlying Hashtable.

See also:
DictionaryBase.OnSet | DictionaryBase.OnInsert

Return to top


Method: IDictionary.Add(
   object key,
   object value
)
Summary
Adds an element with the specified key and value into the DictionaryBase.
C# Syntax:
void IDictionary.Add(
   object key,
   object value
);
Parameters:

key

The key of the element to add.

value

The value of the element to add.

Exceptions
Exception Type Condition
ArgumentNullException key is null.
ArgumentException An element with the same key already exists in the DictionaryBase.
NotSupportedException The DictionaryBase is read-only.

-or-

The DictionaryBase has a fixed size.

Implements:
IDictionary.Add
Remarks
An object that has no correlation between its state and its hash code value should typically not be used as the key. For example, String objects are better than StringBuilder objects for use as keys.

The DictionaryBase.System.Collections.IDictionary.Item property can also be used to add new elements by setting the value of a key that does not exist in the DictionaryBase. For example: myCollection["myNonexistentKey"] = myValue . However, if the specified key already exists in the DictionaryBase, setting the DictionaryBase.System.Collections.IDictionary.this property overwrites the old value. In contrast, the DictionaryBase.System.Collections.IDictionary.Add method does not modify existing elements.

See also:
DictionaryBase.System.Collections.IDictionary.Remove | DictionaryBase.System.Collections.IDictionary.this | IDictionary.Add

Return to top


Method: IDictionary.Contains(
   object key
)
Summary
Determines whether the DictionaryBase contains a specific key.
C# Syntax:
bool IDictionary.Contains(
   object key
);
Parameters:

key

The key to locate in the DictionaryBase.

Return Value:
true if the DictionaryBase contains an element with the specified key; otherwise, false.
Exceptions
Exception Type Condition
ArgumentNullException key is null.
Implements:
IDictionary.Contains
Remarks
This implementation is close to O(1) in most cases.

Return to top


Method: IDictionary.Remove(
   object key
)
Summary
Removes the element with the specified key from the DictionaryBase.
C# Syntax:
void IDictionary.Remove(
   object key
);
Parameters:

key

The key of the element to remove.

Exceptions
Exception Type Condition
ArgumentNullException key is null.
NotSupportedException The DictionaryBase is read-only.

-or-

The DictionaryBase has a fixed size.

Implements:
IDictionary.Remove
Remarks
If the DictionaryBase does not contain an element with the specified key, the DictionaryBase remains unchanged. No exception is thrown.
See also:
DictionaryBase.System.Collections.IDictionary.Add | IDictionary.Remove

Return to top


Method: IEnumerable.GetEnumerator()
Summary
Returns an IEnumerator that can iterate through the DictionaryBase.
C# Syntax:
IEnumerator IEnumerable.GetEnumerator();
Return Value:
An IEnumerator for the DictionaryBase.
Implements:
IEnumerable.GetEnumerator
Remarks
Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.

IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.

After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.

The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

See also:
IDictionaryEnumerator | IEnumerator

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.