Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Removes the entries with the specified key from NameObjectCollectionBase

using System; using System.Collections; using System.Collections.Specialized; public class MyCollection : NameObjectCollectionBase  {    private DictionaryEntry _de = new DictionaryEntry();    public DictionaryEntry this[ int index ]  {       get  {          _de.Key = this.BaseGetKey( index );          _de.Value = this.BaseGet( index );          return( _de );       }    }    public MyCollection( IDictionary d )  {       foreach ( DictionaryEntry de in d )  {          this.BaseAdd( (String) de.Key, de.Value );       }    }    public void Remove( String key )  {       this.BaseRemove( key );    }    public void Remove( int index )  {       this.BaseRemoveAt( index );    } } public class SamplesNameObjectCollectionBase  {    public static void Main()  {       IDictionary d = new ListDictionary();       d.Add( "A", "a" );       d.Add( "B", "b" );       d.Add( "C", "C" );       MyCollection myCol = new MyCollection( d );       Console.WriteLine( "Initial state of the collection (Count = {0}):", myCol.Count );       PrintKeysAndValues( myCol );       myCol.Remove( 1 );       Console.WriteLine( "After removing the element at index 1 (Count = {0}):", myCol.Count );       PrintKeysAndValues( myCol );       myCol.Remove( "red" );       Console.WriteLine( "After removing the element with the key \"red\" (Count = {0}):", myCol.Count );       PrintKeysAndValues( myCol );    }    public static void PrintKeysAndValues( MyCollection myCol )  {       for ( int i = 0; i < myCol.Count; i++ )  {          Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value );       }    } }