- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add key and value into StringDictionary in C#
To add key and value into StringDictionary, the code is as follows −
Example
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() { StringDictionary strDict = new StringDictionary(); strDict.Add("A", "John"); strDict.Add("B", "Andy"); strDict.Add("C", "Tim"); strDict.Add("D", "Ryan"); strDict.Add("E", "Kevin"); strDict.Add("F", "Katie"); strDict.Add("G", "Brad"); Console.WriteLine("StringDictionary elements..."); foreach(DictionaryEntry de in strDict) { Console.WriteLine(de.Key + " " + de.Value); } } }
Output
This will produce the following output −
StringDictionary elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad
Example
Let us see another example −
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() { StringDictionary strDict = new StringDictionary(); strDict.Add("1", "Electric Cars"); strDict.Add("2", "SUV"); strDict.Add("3", "AUV"); Console.WriteLine("StringDictionary elements..."); foreach(DictionaryEntry de in strDict) { Console.WriteLine(de.Key + " " + de.Value); } } }
Output
This will produce the following output −
StringDictionary elements... 1 Electric Cars 2 SUV 3 AUV
Advertisements