Dictionary
Like our normal dictionary it consist of an index and with reference to index it has one more values. (Apni oxford dictionary and c# k dictionary m koe anter nhi h).
Initialize A dictionary Object in C#
Same way,
The syntax goes like
Dictionary <string, string> myDictionary = new Dictionary<string, string>();
Here,
myDictionary is the name of our dictionary,
<string, string> specifies that the index and value will be in string format respectively.
Add Pairs in Dictionary Object :
Then simply we do,
myDictionary.Add("A","Apple");
//Here we have added a index of "A" which is the key and the value assigned to it is Apple.
So, if you refer to "A" then it will return Apple as the value . ( A for Apple).
Delete Pairs in Dictionary :
Then,
myDictionary.Remove("A");
This will delete the index "A" and corresponding values.
Yeah, I think you know more words that start with "A" so, if you want to add more then just one entry per index then make use of List<string> .
The syntax goes as
Dictionary<string, List<string>> myDictionary = new Dictionary<string, List<string>>();
No comments:
Post a Comment