xxxxxxxxxx
Dictionary<string, int> myDictionary = new Dictionary<string, int>()
{
{ "key1", 1 },
{ "key2", 2 },
{ "key3", 3 }
};
// Alternatively as a var:
var myVarDictionary = new Dictionary<string, int>()
{
{ "key1", 1 },
{ "key2", 2 },
{ "key3", 3 }
};
// Check Values:
myDictionary.Add("key4", 4);
myDictionary.Remove("key1");
int value = myDictionary["key2"];
if(myDictionary.ContainsKey("key4")){
// ...
}
xxxxxxxxxx
// Create a dictionary with 2 string values
Dictionary<string, string> capitalOf = new Dictionary<string, string>();
// Add items to the dictionary
capitalOf.Add("Japan", "Tokio");
capitalOf.Add("Portugal", "Lissabon");
// Loop over the dictionary and output the results to the console
foreach (KeyValuePair<string, string> combi in capitalOf)
{
Console.WriteLine("The capital of " + combi.Key + " is " + combi.Value);
}
xxxxxxxxxx
var students2 = new Dictionary<int, StudentName>()
{
[111] = new StudentName { FirstName="Sachin", LastName="Karnik", ID=211 },
[112] = new StudentName { FirstName="Dina", LastName="Salimzianova", ID=317 } ,
[113] = new StudentName { FirstName="Andy", LastName="Ruth", ID=198 }
};
xxxxxxxxxx
A Dictionary is a data structure representing a collection of
key-value pairs. Each key in the Dictionary must be unique.
using System;
using System.Collections.Generic;
namespace CodeTest
{
public class Program
{
static void Main(string[] args)
{
var currencies = new Dictionary<string, string>();
currencies["USA"] = "USD";
currencies["Bangladesh"] = "BDT";
currencies["Belgium"] = "EUR";
Console.WriteLine(currencies["USA"]);
}
}
}
xxxxxxxxxx
// C# program to illustrate Dictionary
using System;
using System.Collections.Generic;
class GFG {
// Main Method
static public void Main()
{
// Creating a dictionary
// using Dictionary<TKey, TValue> class
Dictionary<string, string> My_dict =
new Dictionary<string, string>();
// Adding key/value pairs in the Dictionary
// Using Add() method
My_dict.Add("a.01", "C");
My_dict.Add("a.02", "C++");
My_dict.Add("a.03", "C#");
foreach(KeyValuePair<string, string> element in My_dict)
{
Console.WriteLine("Key:- {0} and Value:- {1}",
element.Key, element.Value);
}
}
}
xxxxxxxxxx
var dict = new Dictionary<string, string>();
// uses dict.Add(key,value);.....
public void Add (TKey key, TValue value);
// be carefull first check it doesen't contains key "one"
public void Clear ();
// remove every thing from dictionary
public bool ContainsKey (TKey key);
// check if key is in the dictionary
public bool ContainsValue (TValue value);
// check if value is in the dictionary
public int EnsureCapacity (int capacity);
//Ensures that the dictionary can hold up to a specified number of
//entries without any further expansion of its backing storage.
public bool Remove (TKey key);
//Removes the value with the specified key
//but first check it contains that key
public void TrimExcess (int capacity);
//Sets the capacity of this dictionary to hold up a specified number
//of entries without any further expansion of its backing storage.
public bool TryAdd (TKey key, TValue value);
// Attempts to add the specified key and value to the dictionary.
public bool TryGetValue (TKey key, out TValue value);
// Gets the value associated with the specified key.
A dictionary is a collection that contains key-value pairs where each key is associated with a value. In C#, the dictionary is implemented by the Dictionary
xxxxxxxxxx
// Create a new dictionary that maps strings to integers
Dictionary<string, int> myDictionary = new Dictionary<string, int>();
// Add some key-value pairs to the dictionary
myDictionary.Add("apple", 1);
myDictionary.Add("banana", 2);
myDictionary.Add("orange", 3);
// Access a value by its key
int bananaValue = myDictionary["banana"]; // returns 2
// Modify a value by its key
myDictionary["orange"] = 4;
// Remove a key-value pair
myDictionary.Remove("apple");
// Check if a key exists in the dictionary
bool containsKey = myDictionary.ContainsKey("apple"); // returns false
// Iterate over the key-value pairs in the dictionary
foreach (KeyValuePair<string, int> pair in myDictionary)
{
string key = pair.Key;
int value = pair.Value;
Console.WriteLine("{0}: {1}", key, value);
}
This code creates a dictionary that maps strings to integers, adds some key-value pairs to the dictionary, accesses and modifies values by their keys, removes a key-value pair, checks if a key exists in the dictionary, and iterates over the key-value pairs in the dictionary.
xxxxxxxxxx
// Creating a Dictionary in C#
Dictionary<string, int> myDictionary = new Dictionary<string, int>();
// Adding key-value pairs to the dictionary
myDictionary.Add("Apple", 1);
myDictionary.Add("Banana", 2);
myDictionary.Add("Orange", 3);
// Accessing values using keys
int appleValue = myDictionary["Apple"];
Console.WriteLine("The value of Apple is: " + appleValue);
// Checking if a key exists in the dictionary
bool isBananaPresent = myDictionary.ContainsKey("Banana");
Console.WriteLine("Is Banana present in the dictionary? " + isBananaPresent);
// Iterating over the dictionary
foreach (KeyValuePair<string, int> kvp in myDictionary)
{
Console.WriteLine("Key: " + kvp.Key + ", Value: " + kvp.Value);
}