Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Leleapplication1
{
Class Program
{
Static Void Main ( String [] ARGs)
{
// Create a new dictionary of strings, with string keys.
//
Dictionary < String , String > Openwith =
New Dictionary < String , String > ();
// Add some elements to the dictionary. There are no
// Duplicate keys, but some of the values are duplicates.
Openwith. Add ( " Txt " , " Notepad.exe " );
Openwith. Add ( " BMP " , " Paint.exe " );
Openwith. Add ( " DiB " , " Paint.exe " );
Openwith. Add ( " RTF " , " Wordpad.exe " );
// The add method throws an exception if the new key is
// Already in the dictionary.
Try
{
Openwith. Add ( " Txt " , " Winword.exe " );
}
Catch (Argumentexception)
{
Console. writeline ( " An element with key = \ " TXT \ " Already exists. " );
}
// The item property is another name for the indexer, So you
// Can omit its name when accessing elements.
Console. writeline ( " For key = \ " RTF \ " , Value = {0 }. " ,
Openwith [ " RTF " ]);
// The indexer can be used to change the value associated
// With a key.
Openwith [ " RTF " ] = " Winword.exe " ;
Console. writeline ( " For key = \ " RTF \ " , Value = {0 }. " ,
Openwith [ " RTF " ]);
// If a key does not exist, setting the indexer for that key
// Adds a new key/value pair.
Openwith [ " Doc " ] = " Winword.exe " ;
// The indexer throws an exception if the requested key is
// Not in the dictionary.
Try
{
Console. writeline ( " For key = \ " TIF \ " , Value = {0 }. " ,
Openwith [ " TIF " ]);
}
Catch (Keynotfoundexception)
{
Console. writeline ( " Key = \ " TIF \ " Is not found. " );
}
// When a program often has to try keys that turn out not
// Be in the dictionary, trygetvalue can be a more efficient
// Way to retrieve values.
String Value = "" ;
If (Openwith. trygetvalue ( " TIF " , Out Value ))
{
Console. writeline ( " For key = \ " TIF \ " , Value = {0 }. " , Value );
}
Else
{
Console. writeline ( " Key = \ " TIF \ " Is not found. " );
}
// Containskey can be used to test keys before inserting
// Them.
If ( ! Openwith. containskey ( " HT " ))
{
Openwith. Add ( " HT " , " Hypertrm.exe " );
Console. writeline ( " Value added for key = \ " HT \ " : {0} " ,
Openwith [ " HT " ]);
}
// When you use foreach to enumerate dictionary elements,
// The elements are retrieved as keyvaluepair objects.
Console. writeline ();
Foreach (Keyvaluepair < String , String > KVp In Openwith)
{
Console. writeline ( " Key = {0}, value = {1} " ,
KVp. Key, kVp. value );
}
//To get the values alone, use the values property.
Dictionary<String,String>. Valuecollection valuecoll=
Openwith. values;
// The elements of the valuecollection are stronugly typed
// With the type that was specified for dictionary values.
Console. writeline ();
Foreach ( String S In Valuecoll)
{
Console. writeline ( " Value = {0} " , S );
}
//To get the keys alone, use the keys property.
Dictionary<String,String>. Keycollection keycoll=
Openwith. Keys;
// The elements of the keycollection are strongly typed
// With the type that was specified for dictionary keys.
Console. writeline ();
Foreach ( String S In Keycoll)
{
Console. writeline ( " Key = {0} " , S );
}
// use the remove method to remove a key/value pair.
console. writeline ( " \ nremove (\ " Doc \ " ) " );
openwith. remove ( " doc " );
If ( ! Openwith. containskey ( " Doc " ))
{
Console. writeline ( " Key \ " Doc \ " Is not found. " );
}
Console. readkey ();
}
}
}