Contact Us

Scripts: C# :: Collections and Generics :: Library Article #14

Developer's Section

Using The Dictionary Object in C#
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (3214)

Learn how to use the Dictionary Object with Generic Data Types in C#

The dictionary Object in C# can help you store data on a key-value container for keeping track of a list of objects (usually given generic type). This is often used when internal memory is required on run time by a C# application to hold internal states and/or data mappings. In the example below we show how to use generics and a few of the methods available for the Dictionary Object.


 Code Snippet 1

//In this example we create a dictionary Object
//with Key type string and value type bool

Dictionary<string, bool> listOfPurchasedCars = new Dictionary<string, bool>() 

  { "Mercedes SLK", false }, 
  { "Toyota Corolla", true }, 
  { "Mazda", false }, 
//Adding a new key value pair
listOfPurchasedCars.add("Chrysler", false);

foreach( string KeyValue in listOfPurchasedCars.Keys){
  //loop thru the Dictionary Keys

foreach( bool ValueOfKey in listOfPurchasedCars.Values){
  //loop thru the Dictionary Values

//Changing the value of a Dictionary Entry (Key)
listOfPurchasedCars["Mercedes SLK"] = true;
listOfPurchasedCars["Mazda"] = false;

See other Scripts in Collections and Generics

Submit Your Scripts:

If you would like to have your Javascripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]