W
William Stacey [MVP]
What is the deal with NameObjectCollectionBase? It allows duplication keys
to be added to the hashtable
using System;
using System.Collections;
using System.Collections.Specialized;
namespace ns
{
/// <summary>
/// Summary description for HashListNO.
/// </summary>
public class HashListNO : NameObjectCollectionBase
{
#region Fields
private DictionaryEntry de = new DictionaryEntry();
private readonly object syncRoot = new object();
#endregion
#region Constructors
public HashListNO()
{
}
public HashListNO( IHashCodeProvider hashProvider, IComparer comparer ) :
base(hashProvider, comparer)
{
}
public HashListNO( IDictionary d, Boolean bReadOnly )
{
foreach ( DictionaryEntry de in d )
{
this.BaseAdd( (String) de.Key, de.Value );
}
this.IsReadOnly = bReadOnly;
}
#endregion
// Gets the syncRoot object for this collection.
public object SyncRoot
{
get { return this.syncRoot; }
}
// Gets a key-and-value pair (DictionaryEntry) using an index.
public DictionaryEntry this[ int index ]
{
get
{
de.Key = this.BaseGetKey(index);
de.Value = this.BaseGet(index);
return( de );
}
}
// Gets or sets the value associated with the specified key.
public Object this[ string key ]
{
get
{
return( this.BaseGet( key ) );
}
set
{
this.BaseSet( key, value );
}
}
// Gets a String array that contains all the keys in the collection.
public String[] AllKeys
{
get
{
return( this.BaseGetAllKeys() );
}
}
// Gets an Object array that contains all the values in the collection.
public Array AllValues
{
get
{
return( this.BaseGetAllValues() );
}
}
// Gets a String array that contains all the values in the collection.
public String[] AllStringValues
{
get
{
return( (string[]) this.BaseGetAllValues( Type.GetType(
"System.String" ) ) );
}
}
// Gets a value indicating if the collection contains keys that are not
null.
public Boolean HasKeys
{
get
{
return( this.BaseHasKeys() );
}
}
// Adds an entry to the collection.
public void Add( string key, object value )
{
this.BaseAdd( key, value );
}
// Removes an entry with the specified key from the collection.
public void Remove( string key )
{
this.BaseRemove( key );
}
// Removes an entry in the specified index from the collection.
public void RemoveAt( int index )
{
this.BaseRemoveAt( index );
}
// Clears all the elements in the collection.
public void Clear()
{
this.BaseClear();
}
}
}
to be added to the hashtable
using System;
using System.Collections;
using System.Collections.Specialized;
namespace ns
{
/// <summary>
/// Summary description for HashListNO.
/// </summary>
public class HashListNO : NameObjectCollectionBase
{
#region Fields
private DictionaryEntry de = new DictionaryEntry();
private readonly object syncRoot = new object();
#endregion
#region Constructors
public HashListNO()
{
}
public HashListNO( IHashCodeProvider hashProvider, IComparer comparer ) :
base(hashProvider, comparer)
{
}
public HashListNO( IDictionary d, Boolean bReadOnly )
{
foreach ( DictionaryEntry de in d )
{
this.BaseAdd( (String) de.Key, de.Value );
}
this.IsReadOnly = bReadOnly;
}
#endregion
// Gets the syncRoot object for this collection.
public object SyncRoot
{
get { return this.syncRoot; }
}
// Gets a key-and-value pair (DictionaryEntry) using an index.
public DictionaryEntry this[ int index ]
{
get
{
de.Key = this.BaseGetKey(index);
de.Value = this.BaseGet(index);
return( de );
}
}
// Gets or sets the value associated with the specified key.
public Object this[ string key ]
{
get
{
return( this.BaseGet( key ) );
}
set
{
this.BaseSet( key, value );
}
}
// Gets a String array that contains all the keys in the collection.
public String[] AllKeys
{
get
{
return( this.BaseGetAllKeys() );
}
}
// Gets an Object array that contains all the values in the collection.
public Array AllValues
{
get
{
return( this.BaseGetAllValues() );
}
}
// Gets a String array that contains all the values in the collection.
public String[] AllStringValues
{
get
{
return( (string[]) this.BaseGetAllValues( Type.GetType(
"System.String" ) ) );
}
}
// Gets a value indicating if the collection contains keys that are not
null.
public Boolean HasKeys
{
get
{
return( this.BaseHasKeys() );
}
}
// Adds an entry to the collection.
public void Add( string key, object value )
{
this.BaseAdd( key, value );
}
// Removes an entry with the specified key from the collection.
public void Remove( string key )
{
this.BaseRemove( key );
}
// Removes an entry in the specified index from the collection.
public void RemoveAt( int index )
{
this.BaseRemoveAt( index );
}
// Clears all the elements in the collection.
public void Clear()
{
this.BaseClear();
}
}
}