public static void Main() { // Creates and initializes a new ListDictionary. ListDictionary myCol = new ListDictionary(); myCol.Add( "Braeburn Apples", "1.49" ); myCol.Add( "Fuji Apples", "1.29" ); myCol.Add( "Gala Apples", "1.49" ); myCol.Add( "Golden Delicious Apples", "1.29" ); myCol.Add( "Granny Smith Apples", "0.89" ); myCol.Add( "Red Delicious Apples", "0.99" ); // Displays the values in the ListDictionary in three different ways. Console.WriteLine( "Initial contents of the ListDictionary:" ); PrintKeysAndValues( myCol ); // Deletes a key. myCol.Remove( "Plums" ); Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" ); PrintKeysAndValues( myCol ); // Clears the entire collection. myCol.Clear(); Console.WriteLine( "The collection contains the following elements after it is cleared:" ); PrintKeysAndValues( myCol ); } public static void PrintKeysAndValues( IDictionary myCol ) { Console.WriteLine( " KEY VALUE" ); foreach ( DictionaryEntry de in myCol ) Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); Console.WriteLine(); }
ListDictionary myCol = new ListDictionary();
myCol.Add( "Braeburn Apples", "1.49" );
myCol.Add( "Fuji Apples", "1.29" );
myCol.Add( "Gala Apples", "1.49" );
myCol.Add( "Golden Delicious Apples", "1.29" );
myCol.Add( "Granny Smith Apples", "0.89" );
myCol.Add( "Red Delicious Apples", "0.99" ); // Displays the values in the ListDictionary in three different ways.
Console.WriteLine( "Initial contents of the ListDictionary:" );
PrintKeysAndValues( myCol ); // Deletes a key.
myCol.Remove( "Plums" );
Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" );
PrintKeysAndValues( myCol ); // Clears the entire collection.
myCol.Clear();
Console.WriteLine( "The collection contains the following elements after it is cleared:" );
PrintKeysAndValues( myCol ); } public static void PrintKeysAndValues( IDictionary myCol ) {
Console.WriteLine( " KEY VALUE" );
foreach ( DictionaryEntry de in myCol )
Console.WriteLine( " {0,-25} {1}", de.Key, de.Value );
Console.WriteLine();
}
ListDictionary ld = new ListDictionary();
ld.Keys.CopyTo(Array ary,0)
ld.Values.CopyTo(Array ary,0)
{
de.Key
de.Value
}
ListDictionary myCol = new ListDictionary();
foreach ( DictionaryEntry de in myCol )
Console.WriteLine("{0} {1}", de.Key, de.Value );