Data Dictionary in c#
1)How to declare data Dictionary:
Dictionary<key, value> mydictionary = new Dictionary<key, value>();
-> Dictionary can be of any data type combination. ex: int, float,string
->First item in the dictionary will be a key and second item will be a value
2)How to add value to dictionary:
mydictionary.Add(key, value);
3)How to retrieve values and keys from the dictionary:
foreach (KeyValuePair<key, value> item in mydictionary)
{
string key = item.Key;
string val = item.Value;
string outputfile = outputfile + key+"\n"+val;
}
4)How to search an item in dictionary
for a value:
mydictionary.ContainsValue(value);
for a key:
mydictionary.ContainsKey( key);
5)To clear value from the dictionary
mydictionary.Clear();
Example:
Dictionary<string, string> dictionary = new Dictionary<string, string>();
string a="Hello";
string b="World";
dictionary.Add(a, b);
foreach (KeyValuePair<string, string> item in dictionary)
{
string key = item.Key;
string val = item.Value;
outputfile = outputfile + "\n" + "\n" + "." + key + "\n" + "{" + "\n" + val;
}
Dictionary<key, value> mydictionary = new Dictionary<key, value>();
-> Dictionary can be of any data type combination. ex: int, float,string
->First item in the dictionary will be a key and second item will be a value
2)How to add value to dictionary:
mydictionary.Add(key, value);
3)How to retrieve values and keys from the dictionary:
foreach (KeyValuePair<key, value> item in mydictionary)
{
string key = item.Key;
string val = item.Value;
string outputfile = outputfile + key+"\n"+val;
}
4)How to search an item in dictionary
for a value:
mydictionary.ContainsValue(value);
for a key:
mydictionary.ContainsKey( key);
5)To clear value from the dictionary
mydictionary.Clear();
Example:
Dictionary<string, string> dictionary = new Dictionary<string, string>();
string a="Hello";
string b="World";
dictionary.Add(a, b);
foreach (KeyValuePair<string, string> item in dictionary)
{
string key = item.Key;
string val = item.Value;
outputfile = outputfile + "\n" + "\n" + "." + key + "\n" + "{" + "\n" + val;
}
Comments
Post a Comment